写入时出现编码错误默认值()至csv.wri文件

2024-09-12 10:23:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用将字典条目列表写入.csv文件csv.writer.writerow文件. 我从一个网站上搜集了这些数据。下面是词典列表的一个示例,其中两个相关条目突出了我的问题。。。你知道吗

dictlist = [{'MBFC Rating': 'Left-Center',  
'MBFC URL': 'https://mediabiasfactcheck.com/beijing-review/',  
'News Outlet': 'Beijing Review',  
'News Outlet URL': 'http://www.bjreview.com/',  
'Notes': 'Notes:\xa0Founded in March 1958\xa0as the weekly Peking Review, it was an important tool for the People’s Republic of China\xa0government to communicate to the rest of world.\xa0Has a Communist, Maoist perspective, but reports new factually.'}, 
{'MBFC Rating': 'Right',
  'MBFC URL': 'https://mediabiasfactcheck.com/daily-sabah/',
  'News Outlet': 'Daily Sabah',
  'News Outlet URL': 'http://www.dailysabah.com/',
  'Notes': 'Notes:\xa0Daily Sabah\xa0is an English, German and Arabic-language daily newpaper published in Turkey\xa0and owned by Turkuvaz Media Group. Foreign Policy\xa0has labeled the Daily Sabah as a mouthpiece of the AKP and especially Recep Tayyip Erdoğan, the current president of Turkey. (Wikipedia) \xa0Reports USA news with a right of center bias.'}]

我最初尝试使用下面的代码来实现这一点,但是

 with open(OUTPUTFILE, 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(sourcedictlist[0].keys())
    for i in range(len(sourcedictlist)):
        writer.writerow(sourcedictlist[i].values())

我最终得到了以下错误,我将其追溯到第二个dict的“Notes”值中“Ergodan”中的“g”符号

UnicodeEncodeError: 'charmap' codec can't encode character '\u011f' in position 240: character maps to <undefined>

然后,我尝试将encoding=“UTF-8”行添加到打开csv文件的行中:

with open(OUTPUTFILE, 'w', newline='', encoding="UTF-8") as f:
    writer = csv.writer(f)
    writer.writerow(sourcedictlist[0].keys())
    for i in range(len(sourcedictlist)):
        writer.writerow(sourcedictlist[i].values(

这样可以消除错误,并完成编写器.writerow循环到csv文件的所有条目,但文字是所有混乱时,我打开我的csv!(见下文)

Notes: Founded in March 1958 as the weekly Peking Review, it was an important tool for the People’s Republic of China government to communicate to the rest of world. Has a Communist, Maoist perspective, but reports new factually.

Notes: Daily Sabah is an English, German and Arabic-language daily newpaper published in Turkey and owned by Turkuvaz Media Group. Foreign Policy has labeled the Daily Sabah as a mouthpiece of the AKP and especially Recep Tayyip Erdoğan, the current president of Turkey. (Wikipedia)  Reports USA news with a right of center bias.

有人能解释一下我做错了什么吗?你知道吗


Tags: and文件ofcsvthetoinan