gpt4 book ai didi

python - 混合类型的编码数据?

转载 作者:太空宇宙 更新时间:2023-11-04 07:03:34 25 4
gpt4 key购买 nike

我有一个数组如下:

row = [u'Arun DC', 4.0, 34.0, 76.0, 223.52941176470588, u'yes', 0.0, '', '', '', '', u'yes', '', u'yes', u'yes', u'yes', u'no', u'\xa37.50 - \xa310 not clear what this is for', u'\xa37.50 - \xa310 not clear what this is for', '', u'\xa37.50 - \xa310 not clear what this is for', u'\xa37.50 - \xa310 not clear what this is for', '', '', '', '', u'no water on sites', '', u'1st years rent free', '']

我想将其写入 CSV 文件,但出现以下错误:

writer.writerow(row)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 0: ordinal not in range(128)

所以我尝试先对其进行编码,但这给了我一个错误,因为我无法对 float 项目进行编码:

writer.writerow([i.encode('latin-1') for i in row])
AttributeError: 'int' object has no attribute 'encode'

有什么想法可以巧妙地解决这个问题吗?

最佳答案

[x.encode('latin-1') if isinstance(x, unicode) else x
for x in row]

关于python - 混合类型的编码数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8050631/

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