当我将一个方法的字符串发送到同一文件中的另一个方法时,出现以下错误:
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 2-3: unexpected end of data
当我在第二种方法中将输入值替换为字符串时,该函数起作用。当我将字符串作为变量发送时,它不是。 当我将循环放入method2时,该函数起作用。 utf8和unicode是否存在编码问题? 当我将列表从method1发送到method2时,它可以工作。当我只发送一个字符串时,它不起作用。
def compareTwoFiles(file1Path, file2Path):
try:
print(type(file2Path) #result is unicode
with open(file2Path, "r") as json_file: # does not work
#with open("./DEV3_export.json", "r") as json_file: # works
fileDict = json.load(json_file)
task_list = fileDict['definition']['tasks']
for i, task_details in enumerate(task_list):
task_details = task_list[i]
print(task_details)
except IOError as e:
logging.error(e.message)
return
def compareFilesInTwoDirectories(serversNameList):
catchedServerPath = './servers/' + file_name + '_export.json'
newServersPath = './' + file_name + '_export.json'
logging.warning(catchedServerPath)
logging.warning(newServersPath)
if os.stat(newServersPath).st_size > 0:
compareTwoFiles(catchedServerPath, newServersPath)
return
print(compareFilesInTwoDirectories(serversNameList))
答案 0 :(得分:0)
通过使用io.open
而不是open
解决了该问题。
with io.open(file2Path) as json_file:
fileDict = json.load(json_file)
task_list = fileDict['definition']['tasks']
for i, task_details in enumerate(task_list):
task_details = task_list[i]
print(task_details)