马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
edit = open('edit.txt', 'r+')
edit.seek(0, 0)
for edit_each_line in edit:
print(edit_each_line)
发生如下报错Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Program Files\JetBrains\PyCharm 2020.1.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2020.1.2\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/admin/Desktop/Python/fix.py", line 3, in <module>
for edit_each_line in edit:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xb9 in position 35: illegal multibyte sequence
py文件和edit.txt在同一个文件夹
是我写错了?
文件编码报错,和代码本身无关,把编码设置为 utf-8,这样试试看,:
edit = open('edit.txt', 'r+',encoding='utf-8')
edit.seek(0, 0)
for edit_each_line in edit:
print(edit_each_line)
|