Simon_xlj 发表于 2020-7-22 23:02:35

文件处理

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在同一个文件夹
是我写错了?

Twilight6 发表于 2020-7-22 23:03:34



文件编码报错,和代码本身无关,把编码设置为 utf-8,这样试试看,:

edit = open('edit.txt', 'r+',encoding='utf-8')
edit.seek(0, 0)
for edit_each_line in edit:
    print(edit_each_line)

zltzlt 发表于 2020-7-23 06:43:02

请见:https://fishc.com.cn/thread-158932-1-1.html
页: [1]
查看完整版本: 文件处理