我在python中为脚本比较编写了一个简单的字符串,从Visual Studio(我使用PTVS)运行时它工作正常,但是当我从命令行执行脚本或只运行它时,出现错误&# 34; utf8编解码器不能解码字节"。我在网站上找到了这个错误,这似乎是一个常见的错误,但没有一个解决方案有帮助。 这是代码:
import json
import sys
import codecs
json_data=open('input_1.json')
data = json.load(json_data)
json_data.close()
names = [line.strip().split(',')[0].strip() for line in open('input_2.txt')]
output = codecs.open('out.txt',"w",'utf-8')
for name in names:
x = [name + " - " + entry["look"] for entry in data if entry['around'] in name]
if(len(x)>0):
output.write(''.join(x)+'\n')
else:
output.write(name + ' - wtf\n')
output.close()