Mil*_*ano 1 python encoding decoding
我正在尝试从网页中获取一些数据。此网页已声明字符集为 utf-8。但是 \xa3 符号有问题。我不能被编码或解码到/从'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)+', '
Run Code Online (Sandbox Code Playgroud)
错误:
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)
Run Code Online (Sandbox Code Playgroud)
请问这有什么问题吗?
编辑:
全方法:
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
Run Code Online (Sandbox Code Playgroud)
str()使用 ASCII 编解码器对 Unicode 对象进行隐式编码。显式编码您的对象,或者不使用str()对象并构建一个 Unicode 字符串:
values_statement += value.encode('utf8') + ', '
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4010 次 |
| 最近记录: |