当我想用Python保存CSV时出现编码问题

wqsoz72f  于 2022-12-06  发布在  Python
关注(0)|答案(1)|浏览(104)

我想用Python将此列表保存为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\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

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

zzwlnbp8

zzwlnbp81#

一种方法是首先将任何bytes转换为utf-8,然后使用.writerow()写入一行。
当使用csv.writer()时,你应该使用newline=''来避免多余的换行符(这在Python文档中有解释)。
例如:

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\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']
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字符串作为单独的字符进行迭代,这就是为什么您会在输出中看到单独的字母。

相关问题