我試圖創建一個沒有標題的重複CSV。當我嘗試這一點,我得到以下錯誤:在Python中將CSV轉換爲UTF-8
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 1895: invalid start byte.
我讀過Unicode
和UTF-8
編碼蟒蛇CSV
documentation,並已付諸實施。 但是,我的輸出文件是在沒有數據的情況下生成的。不知道我在這裏做錯了什麼。
import csv
path = '/Users/johndoe/file.csv'
with open(path, 'r') as infile, open(path + 'final.csv', 'w') as outfile:
def unicode_csv(infile, outfile):
inputs = csv.reader(utf_8_encoder(infile))
output = csv.writer(outfile)
for index, row in enumerate(inputs):
yield [unicode(cell, 'utf-8') for cell in row]
if index == 0:
continue
output.writerow(row)
def utf_8_encoder(infile):
for line in infile:
yield line.encode('utf-8')
unicode_csv(infile, outfile)