简体   繁体   English

Python,ASCII无法编码字节错误

[英]Python, ascii can't encode byte error

Hi I have an abstract that I download from a website, sometimes it doesn't have special chars and it doesn't give me any problem when I try to store it in the db... 嗨,我有一个摘要,可以从网站上下载,有时没有特殊字符,当我尝试将其存储在数据库中时也没有任何问题...

Other times I get an error, Python can't encode byte error blah blah blah... 其他时候我遇到错误,Python无法编码字节错误等等等等……

I use .encode('ascii', 'ignore') in order to avoid those issues but in the end those special characters don't get saved... what should I use to store every character without problems?? 为了避免这些问题,我使用了.encode('ascii','ignore'),但是最后这些特殊字符没有得到保存...我应该使用什么来存储每个没有问题的字符?

As Alex mentioned, you first need to determine how the file is encoded ( response.encoding in scrapy). 亚历克斯提到的,你首先需要确定该文件的编码方式( response.encoding在scrapy)。 Assuming UTF-8, you'd simply .encode('utf-8') , or any of Python's other supported encodings . 假设使用UTF-8,您只需使用.encode('utf-8')或Python其他受支持的编码

You also need to make sure that the table in which you are trying to store the data accepts that particular encoding. 您还需要确保要在其中存储数据的表接受该特定编码。 See this question for examples on how to enable UTF-8 storage. 有关如何启用UTF-8存储的示例,请参见此问题 You can also use any of the other character sets that MySQL supports in the same manner. 您也可以以相同的方式使用MySQL支持的任何其他字符集

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM