简体   繁体   English

用 utf-8 (django + pandas) 解码 csv 文件

[英]decode csv file in utf-8 (django + pandas)

My code generates a csv file, returns it to the browser.我的代码生成一个 csv 文件,将其返回给浏览器。 After downloading the file, all Russian words look like: РђР ± Р ° РєР ° РЅ ... encoding as if it doesn't work.下载文件后,所有俄语单词看起来像:РђР ± Р ° РєР ° РЅ ... 编码好像不起作用。 Tell me, please, how can I solve the problem?请告诉我,我该如何解决这个问题?

dataCsv = data.to_csv(sep=';', header='True', decimal=',', encoding='utf-8')
response = HttpResponse(dataCsv, content_type="text/csv")
response['Content-Disposition'] = 'attachment; filename=data.csv'

return response

i think you should use 'cp869 Codec' for Russian我认为您应该对俄语使用“cp869 Codec”

check the link for more details: https://docs.python.org/2.4/lib/standard-encodings.html查看链接了解更多详情: https : //docs.python.org/2.4/lib/standard-encodings.html

I recently had an issue with UTF-8 and CSV, and I found a fix after much searching.我最近遇到了 UTF-8 和 CSV 的问题,经过多次搜索后我找到了解决方法。 I can't recall to credit the original, but what I did was add:我不记得归功于原文,但我所做的是添加:

    response.write(u'\ufeff'.encode('utf8'))

after setting up the response.设置响应后。 So you'd have:所以你会有:

response = HttpResponse(dataCsv, content_type="text/csv")
response['Content-Disposition'] = 'attachment; filename=data.csv'
response.write(u'\ufeff'.encode('utf8'))

return response

From my understanding, this tells Excel "This is a UTF-8 encoded file".根据我的理解,这告诉 Excel“这是一个 UTF-8 编码的文件”。

In my settings file i'm insert在我的设置文件中,我插入

DEFAULT_CHARSET = 'cp1251'

Views:意见:

dataCsv = data.to_csv(sep=';', header='True', decimal=',', encoding='cp1251')
response = HttpResponse(dataCsv, content_type="text/csv")
response['Content-Disposition'] = 'attachment; filename=data.csv'

return response

暂无
暂无

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

相关问题 在 Pandas 中解码为 UTF-8 - Decode to UTF-8 in pandas 有没有办法在熊猫中将 csv 文件编码为 UTF-8? - Is there a way to encode a csv file to UTF-8 in pandas? 错误 n 读取 csv 文件:utf-8 编解码器无法解码 - Error n reading csv file: utf-8 codec cant decode “ utf-8”编解码器无法解码位置中的字节0x96…当通过熊猫read_csv读取文本文件时 - 'utf-8' codec can't decode byte 0x96 in position … when reading text file by pandas read_csv UnicodeDecodeError: 'utf-8' 编解码器无法解码位置 1 的字节 0x8b:无效的起始字节,同时在 Pandas 中读取 csv 文件 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte, while reading csv file in pandas UnicodeDecodeError when reading CSV file in Pandas with Python “'utf-8' codec can't decode byte 0xff in position 0: invalid start byte” - UnicodeDecodeError when reading CSV file in Pandas with Python “'utf-8' codec can't decode byte 0xff in position 0: invalid start byte” 通过 Paramiko 从 SFTP 服务器将 CSV 文件读入 Pandas 失败,“'utf-8'编解码器无法解码字节......在位置......:无效的起始字节” - Reading CSV file into Pandas from SFTP server via Paramiko fails with “'utf-8' codec can't decode byte … in position …: invalid start byte” python读取文件utf-8解码问题 - python read file utf-8 decode issue Python 3 CSV 文件给出 UnicodeDecodeError: 'utf-8' 编解码器在打印时无法解码字节错误 - Python 3 CSV file giving UnicodeDecodeError: 'utf-8' codec can't decode byte error when I print Python 3 - 读取 utf-8 编码的 csv 到 pandas - Python 3 - reading utf-8 encoded csv into pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM