无法向文件写入数据

问题描述 投票:0回答:1

我将数据存储在一个文件中 - 分隔符选项卡 {'id': '123', 'name': 'pečnostní informační služba'}

当我尝试使用 python 代码读取数据并在第二个文件中写入数据时出现错误

  with open(output_file, 'w') as f_output, open(input_file,encoding = 'utf-8-sig') as f_input:

        reader = csv.DictReader(f_input,delimiter='\t')
        fieldnames =  reader.fieldnames


        writer = csv.DictWriter(f_output, fieldnames=fieldnames)
        writer.writeheader()

        for row in reader:
            print(row)
    
            writer.writerow(row)


Traceback (most recent call last):
  File "C:\Python_Projects\Python_extra_code\csv_example.py", line 180, in <module>
    writer.writerow(row)
  File "C:\Dev\Python3.11\Lib\csv.py", line 154, in writerow
    return self.writer.writerow(self._dict_to_list(rowdict))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Dev\Python3.11\Lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode character '\u010d' in position 69: character maps to <undefined>
python python-3.x python-unicode
1个回答
0
投票

您的输出文件使用“cp1252”编码。此编码没有您要保存的 unicode 字符

č
。您可以通过运行这两行来复制错误:

text = '\u010d' 
text.encode('cp1252')

尝试通过保存为 utf-8 编码来修复此错误:

with open(output_file, 'w',encoding='utf-8') as f_output:
    text = '\u010d' 
    f_output.write(text)

或者允许忽略错误,同时保存解码错误的字符:

with open(output_file, 'w',encoding='cp1252', errors='ignore') as f_output:
    text = '\u010d' 
    f_output.write(text)
© www.soinside.com 2019 - 2024. All rights reserved.