我正在使用以下代码来打印contact.dat的内容,但是我收到错误。
import pickle
def main():
infile = open("contacts.dat",'wb')
file_contents = infile.read()
infile.close()
print(file_contents)
main()
File "C:\Python31\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 36: character maps to <undefined>
如何获取读取和打印文件的代码?
答案 0 :(得分:1)
在黑暗中拍摄,但我通常使用readlines,以及'with open ..':
with open("contacts.dat") as infile:
file_contents = infile.readlines()
print(file_contents)
试试吗?