我的影片格式混亂的一個文件,如果我嘗試用簡單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