本文介绍了codexa3(磅符号)的EncodeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从网页中获取一些数据。该网页已声明charset为utf-8。但是\xa3号存在问题。我无法在'utf-8'中进行编码或解码。
I'm trying to get some data from a web page. This webpage has stated that charset is utf-8. But there is a problem with \xa3 sign. I can't be encoded or decoded into/from 'utf-8'.
for key,value in self.__dict__.iteritems():
if key not in self.db_attributes:
print repr(value)
attrs_statement+=str(key)+', '
values_statement+=str(value)+', '
错误:
u'\xa3410'
Traceback (most recent call last):
File "C:\Users\Milano\My Documents\LiClipse Workspace\Velvet_scraper\vehicle.py", line 432, in <module>
v.prepare_insert_statement('motorhog_temp')
File "C:\Users\Milano\My Documents\LiClipse Workspace\Velvet_scraper\vehicle.py", line 381, in prepare_insert_statement
values_statement+=str(value)+', '
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 0: ordinal not in range(128)
请问这是怎么回事?
编辑:
整个方法:
def prepare_insert_statement(self,table):
log('prepare_insert_statement, table: {0}'.format(table))
attrs_statement = "("
values_statement = "("
for key,value in self.__dict__.iteritems():
if key not in self.db_attributes:
print repr(value)
attrs_statement+=key+', '
values_statement+=value+', '
attrs_statement+=')'
values_statement+=')'
statement = """INSERT INTO TABLE {0}{1} VALUES{2}""".format(table,attrs_statement,values_statement)
return statement
推荐答案
str()
使用ASCII编解码器隐式编码Unicode对象。显式编码您的对象,或者不使用 str()
对象而是建立Unicode字符串:
str()
implicitly encodes Unicode objects with the ASCII codec. Explicitly encode your object, or do not use str()
objects and build up a Unicode string instead:
values_statement += value.encode('utf8') + ', '
这篇关于codexa3(磅符号)的EncodeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!