[英]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.