EncodeError 与 \xa3(井号)
EncodeError with \xa3 (pound sign)
我正在尝试从网页中获取一些数据。该网页已声明字符集为utf-8。但是 \xa3 符号有问题。我无法编码或解码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 字符串:
values_statement += value.encode('utf8') + ', '
我正在尝试从网页中获取一些数据。该网页已声明字符集为utf-8。但是 \xa3 符号有问题。我无法编码或解码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 字符串:
values_statement += value.encode('utf8') + ', '