我的影片格式混乱的一个文件,如果我尝试用简单open('data.csv', 'r')
我得到打开它:采用分体式时收到错误()
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in
position 4681: character maps to <undefined>
所以我用open('data.csv', 'rb')
代替,它工作正常。然后我试图使用所需要的信息转移到一个新的文件没有成功:
with open('datacsv', 'rb') as file, open('new.csv', 'w') as newf:
for f in file:
newf.write(str(f.split(',')[0:5]))
如果我脱掉它的数据写入到新文件罚款split()
,但如果我加分我使用的提取前几个栏我得到:
TypeError: 'str' does not support the buffer interface
我试过的建议在这里TypeError: 'str' does not support the buffer interface但他们没有帮助。
如何避免TypeError
上升?
你使用的是什么版本的Python?你可以尝试python3,r,而不是rb。 – pvg
这是python3,如果我没有'rb'文件将不会打开。 – Leb
这是你的问题,而不是类型错误。你需要弄清楚文件的编码是什么,然后打开编码,就像open(filename,encoding =“utf8”)'一样。改为rb只是掩盖你的根本问题。 – pvg