简体   繁体   English

csv.writer 编码“utf-8”,但读取编码“cp1252”

[英]csv.writer encoding 'utf-8', but reading encoding 'cp1252'

When writing to a file I use the following code.写入文件时,我使用以下代码。 Here it's upper case, but I've also seen the encoding in lower case utf-8 .这里是大写,但我也看到了小写utf-8的编码。

path_to_file = os.path.join(r'C:\Users\jpm\Downloads', 'c19_Vaccine_Current.csv')

#write to file
with open(path_to_file, 'w', newline='', encoding='UTF-8') as csvfile:
    f = csv.writer(csvfile) 
    #write the headers of the csv file
    f.writerow(['County','AdminCount','AdminCountChange', 'RollAvg', 'AllocDoses', 'FullyVaccinated',                    'FullyVaccinatedChange', 'ReportDate', 'Pop', 'PctVaccinated', 'LHDInventory', 'CommInventory',
                'TotalInventory', 'InventoryDate'])

And to check if the *.csv is in fact utf-8 I open it and read it:为了检查 *.csv 实际上是utf-8我打开它并阅读它:

with open(path_to_file, 'r') as r:
    print(r)

I'm expecting the encoding to be utf-8 , but I get:我期望编码为utf-8 ,但我得到:

<_io.TextIOWrapper name='C:\\Users\\jpm\\Downloads\\c19_Vaccine_Current.csv' mode='r' encoding='cp1252'>

I pretty much borrowed the code from this answer .我几乎从这个答案中借用了代码。 And I've also read the doc .而且我还阅读了文档 It's crucial that I have the *.csv file as utf-8 , but that doesn't appear to be the case.至关重要的是,我将 *.csv 文件作为utf-8 ,但情况似乎并非如此。

The encoding has to be specified on an open as well.编码也必须在打开时指定。 The encoding in which a file is opened is platform dependant, it would seem to be cp1252 on windows.打开文件的编码取决于平台,它似乎是 windows 上的 cp1252。

You can check the default platform encoding with this: (on Mac it gives utf-8)您可以使用以下命令检查默认平台编码:(在 Mac 上,它提供 utf-8)

>>>import locale
>>>locale.getpreferredencoding(False)
'UTF-8'
with open('file', 'r', encoding='utf-8'):
    ...
with open('file', 'w', encoding='utf-8'):
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (Python) Beautifull soup 和编码 (utf-8, cp1252,ascii…) - (Python) Beautifull soup and encoding (utf-8, cp1252,ascii…) 将 python 3.7 默认编码从 cp1252 更改为 cp65001 aka UTF-8 - Change python 3.7 default encoding from cp1252 to cp65001 aka UTF-8 如何可靠地猜测 MacRoman、CP1252、Latin1、UTF-8 和 ASCII 之间的编码 - How to reliably guess the encoding between MacRoman, CP1252, Latin1, UTF-8, and ASCII Quickbase API以CP1252编码返回数据,但表示它返回UTF-8 - Quickbase API returns data in CP1252 encoding but says it's returning UTF-8 Python:来自多个CSV的多个数据帧,将cp1252编码为utf8 - Python: Multiple dataframes from multiple CSV, encoding cp1252 to utf8 Python 以 utf-8 或 cp1252 格式导入 .csv 文件 - Python importing .csv files in utf-8 or cp1252 Python 3 默认编码 cp1252 - Python 3 Default Encoding cp1252 使用python导出csv文件时如何将cp1252转换为UTF-8 - How to convert cp1252 to UTF-8 when export csv file using python glob error &lt;_io.TextIOWrapper name =&#39;...&#39;mode =&#39;r&#39;scoding =&#39;cp1252&#39;&gt;读取文本文件错误 - glob error <_io.TextIOWrapper name='…' mode='r' encoding='cp1252'> reading text file error 与cp1252 consoless上utf-8中的进程通信 - communicate with a process in utf-8 on a cp1252 consoless
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM