gpt4 book ai didi

python 2.7编码解码

转载 作者:太空狗 更新时间:2023-10-30 02:13:17 26 4
gpt4 key购买 nike

我有一个涉及编码/解码的问题。我从文件中读取文本并将其与数据库 (Postgres) 中的文本进行比较比较在两个列表中完成

从文件中我得到“još”的“jo\x9a”,从数据库中我得到相同值的“jo\xc5\xa1”

common = [a for a in codes_from_file if a in kode_prfoksov]

# Items in one but not the other
only1 = [a for a in codes_from_file if not a in kode_prfoksov]

#Items only in another
only2 = [a for a in kode_prfoksov if not a in codes_from_file ]

如何解决?比较这两个字符串应该设置哪个编码来解决问题?

谢谢

最佳答案

第一个好像是windows-1250,第二个是utf-8

>>> print 'jo\x9a'.decode('windows-1250')
još
>>> print 'jo\xc5\xa1'.decode('utf-8')
još
>>> 'jo\x9a'.decode('windows-1250') == 'jo\xc5\xa1'.decode('utf-8')
True

关于python 2.7编码解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9801953/

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