Очевидно, что ваша проблема заключается в файлах JSON .. Но какой ?? где ??
Если вы добавите предложение try/except
, вы можете войти в систему и посмотреть, какие файлы вызывают проблему, поэтому вы можете немного легче отладить.
import json
import sys
import re
import os
reload(sys)
sys.setdefaultencoding('utf8')
path = '/Users/.../' (The actual path is in my code)
textfiles = []
for root, dirs, files in os.walk(r'/Users/.../'):
for file in files:
if file.endswith(".txt"):
textfiles.append(file)
for filename in textfiles:
try:
with open(path + filename) as json_data:
data = json.load(json_data)
opinion = data['plain_text']
f = open(path + filename, 'w')
f.write(opinion)
f.close()
except ValueError as e:
print(filename) # Gives the filename
print(e) # Gives the location of the problem in the file
Кроме того, он должен продолжать перебирать файлы и выдавать ошибки на проблемные.
Вы даже можете сохранить в log.txt для последующей обработки.
вот небольшой пример
lst = [1, 2, '3', 4, 5]
for i in lst:
try:
l = 123 + i
print(l)
except Exception as e:
print(e)
выход:
124
125
unsupported operand type(s) for +: 'int' and 'str'
127
128