【问题标题】:Encoding problem when I want to save a CSV in Python当我想在 Python 中保存 CSV 时出现编码问题
【发布时间】:2022-11-01 20:18:22
【问题描述】:

我想将此列表保存在 Python 中的 CSV 文件中。

['4CIIN', '04-11-2022', '00:00', '2022-11-05 00:00:00', '00:00', b'4CIIN Compiti Scrivi un programma C che, 3CIIN letto l\xe2\x80\x99anno di nascita di una persona e l\xe2\x80\x99anno attuale in input, calcoli l\xe2\x80\x99et\xc3\xa0 della persona. Restituisci un opportuno messaggio se la persona \xc3\xa8 maggiorenne o no.   - From Google', '0']

使用此代码

with open('salva.csv','w', newline='\n', encoding='utf-8') as f:
    write=csv.writer(f)
    write.writerows(row)

我的结果似乎不正确

4,C,I,I,N
0,4,-,1,1,-,2,0,2,2
0,0,:,0,0
2,0,2,2,-,1,1,-,0,5, ,0,0,:,0,0,:,0,0
0,0,:,0,0
52,67,73,73,78,32,67,111,109,112,105,116,105,32,83,99,114,105,118,105,32,117,110,32,112,114,111,103,114,97,109,109,97,32,67,32,99,104,101,44,32,51,67,73,73,78,32,108,101,116,116,111,32,108,226,128,153,97,110,110,111,32,100,105,32,110,97,115,99,105,116,97,32,100,105,32,117,110,97,32,112,101,114,115,111,110,97,32,101,32,108,226,128,153,97,110,110,111,32,97,116,116,117,97,108,101,32,105,110,32,105,110,112,117,116,44,32,99,97,108,99,111,108,105,32,108,226,128,153,101,116,195,160,32,100,101,108,108,97,32,112,101,114,115,111,110,97,46,32,82,101,115,116,105,116,117,105,115,99,105,32,117,110,32,111,112,112,111,114,116,117,110,111,32,109,101,115,115,97,103,103,105,111,32,115,101,32,108,97,32,112,101,114,115,111,110,97,32,195,168,32,109,97,103,103,105,111,114,101,110,110,101,32,111,32,110,111,46,32,32,32,45,32,70,114,111,109,32,71,111,111,103,108,101
0

我需要一些基础知识来了解如何以正确的方式转换字符串,并添加分隔符。

【问题讨论】:

  • 您的列表混合了字符串和字节字符串...
  • 除了前面的注释,对单个列表使用writerow,而不是writerows

标签: python csv encoding


【解决方案1】:

一种方法是首先将任何bytes 转换为utf-8。然后使用.writerow() 写一行。

使用csv.writer() 时,您应该使用newline='' 以避免多余的不需要的换行符(this is explained in the Python docs)。

例如:

import csv

row = ['4CIIN', '04-11-2022', '00:00', '2022-11-05 00:00:00', '00:00', b'4CIIN Compiti Scrivi un programma C che, 3CIIN letto l’anno di nascita di una persona e l’anno attuale in input, calcoli l’età della persona. Restituisci un opportuno messaggio se la persona è maggiorenne o no.   - From Google', '0']
row_str = [v.decode('utf-8') if type(v) == bytes else v for v in row]

with open('salva.csv','w', newline='', encoding='utf-8') as f_output:
    csv_output =  csv.writer(f_output)
    csv_output.writerow(row_str)

给你一行:

4CIIN,04-11-2022,00:00,2022-11-05 00:00:00,00:00,"4CIIN Compiti Scrivi un programma C che, 3CIIN letto l’anno di nascita di una persona e l’anno attuale in input, calcoli l’età della persona. Restituisci un opportuno messaggio se la persona è maggiorenne o no.   - From Google",0

注意:您需要使用可以正确显示 utf-8 编码的应用程序才能正确查看。

您的代码使用了.writerows()。这将尝试遍历列表中的每个条目并为每个条目创建一行。例如,它会将您的 4CIIN 字符串作为单独的字符进行迭代,这就是您在输出中看到单个字母的原因。

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    相关资源
    最近更新 更多