UnicodeDecodeError:“ utf-8”编解码器无法解码位置1的字节0xb3:无效的起始字节

时间:2019-08-12 07:05:31

标签: python csv unicode decode

我有选择特定词形式csv的代码,并创建了另一个包含该词的csv文件。

我已经通过将某些编码样式从utf-8更改为utf-16来更改了一些代码,但是都无法使用。

#-*- coding:utf-8-*-
import csv

input_filename = '/Users/chanhee.kang/Desktop/exit.csv'
output_filename = '/Users/chanhee.kang/Desktop/exit_mefge.csv'
reader = csv.reader(open(input_filename, 'r', encoding='utf-8'), delimiter=';')

input_header = next(reader)
columns_to_keep = []
for i, name in enumerate(input_header):
    if 'startup' in name:
        columns_to_keep.append(i)

writer = csv.writer(open(output_filename, 'w'), delimiter=';')
output_header = []
for column_index in columns_to_keep:
    output_header.append(input_header[column_index])

writer.writerow(output_header)

for row in reader:
    new_row = []
    for column_index in columns_to_keep:
        new_row.append(row[column_index])
    writer.writerow(new_row)

这些是以下错误:

Traceback (most recent call last):
  File "/Users/chanhee.kang/Desktop/word.py", line 10, in <module>
    input_header = next(reader)
  File "/Users/chanhee.kang/anaconda3/lib/python3.7/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 1: invalid start byte
[Finished in 0.089s]

0 个答案:

没有答案