gpt4 book ai didi

python - EncodeError with\xa3(井号)

转载 作者:太空宇宙 更新时间:2023-11-04 10:21:59 28 4
gpt4 key购买 nike

我正在尝试从网页中获取一些数据。该网页已声明字符集为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)+', '

错误:

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') + ', '

关于python - EncodeError with\xa3(井号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31746333/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com