在python代码中编码UTF8字符时出现UTF8错误。它们显示为UTF8

2024-06-28 20:10:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一张单子

[[“由于本周末的风暴,我们已将Blumenfield自行车骑行时间改为2月26日。希望能在那里见到您。\xe2\x80\xa6',“本周末阳光充足,请乘坐海滩巴士,从伍德兰山到海滩,只需$\xe2\x80\xa6'],[“RT@LHansenLA:昨天在@LAPPL@eaglendbadge新的手表结束纪念墙钻机内窥见一眼。向堕落的@LAPD w/\xe2\x80\xa6'',“很高兴加入Art Sherman和Winds Over@Wendys,向退伍军人致敬,以及Ron和\xe2\x80\xa6'”主持的15年每周例会],“与我一起参加第四届Blumenfield自行车赛。享受两个轮子的西谷。回复:'“]]

如您所见,不幸的是,列表显示的是文本UTF-8,而不是字符本身。在我的代码里用UTF编码

outtweets = [[str(tweet.text.encode("utf-8"))] for tweet in correct_date_tweet]            
            outtweets = [[stuff.replace("b\'", "")] for sublist in outtweets for stuff in sublist]
            outtweets = [[stuff.replace('b\"', "")] for sublist in outtweets for stuff in sublist]

以上代码是删除b前缀所必需的。这些不可能出现在我的tweets中,因为我正在进行机器学习分析,并受到bs的影响。在

我的问题

How do I replace the UTF-8 script with the actual characters?

我需要以某种方式对其进行编码,因为我从(3个城市)x(50名官员)x(每个城市12个月的微博)中提取推文,因此尝试手动替换它们的效率是不可能的。在

代码

^{pr2}$

更新

根据一个答案,我尝试将其中一行改为outtweets = [[tweet.text] for tweet in correct_date_tweet]

但这并没有起作用,因为它产生了

---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
<ipython-input-12-a864b5efe8af> in <module>()
----> 1 get_all_tweets("BobBlumenfield","instance file")

<ipython-input-9-d0b9b37c7261> in get_all_tweets(screen_name, mode)
    104             with open(os.path.join(save_location,'%s.instance' % screen_name), mode ='w') as f:
    105                 writer = csv.writer(f)
--> 106                 writer.writerows(outtweets)
    107         else:
    108             with open(os.path.join(save_location,'%s.csv' % screen_name), 'w',encoding='utf-8') as f:

C:\Users\Stan Shunpike\Anaconda3\lib\encodings\cp1252.py in encode(self, input, final)
     17 class IncrementalEncoder(codecs.IncrementalEncoder):
     18     def encode(self, input, final=False):
---> 19         return codecs.charmap_encode(input,self.errors,encoding_table)[0]
     20 
     21 class IncrementalDecoder(codecs.IncrementalDecoder):

UnicodeEncodeError: 'charmap' codec can't encode characters in position 64-65: character maps to <undefined>

Tags: 代码inforinputreplacetweetsutftweet
1条回答
网友
1楼 · 发布于 2024-06-28 20:10:14

删除以下行:

outtweets = [[str(tweet.text.encode("utf-8"))] for tweet in correct_date_tweet] 

原因如下:

  1. 你在编码一个字节串。因此b。在
  2. 您正在使用未定义编码的str。在这种模式下,您将获得数组的表示形式,其中包括类型,这也是b和UTF-8转义。在
  3. 不需要在代码中间进行编码。仅在写入文件或网络时编码(打印时不编码)。如果使用open()的内置编码器,则很少需要自己调用.encode()。在

在文本模式下使用open()时,请始终指定编码,因为每个平台的编码不同。在

从代码中删除.encode()的所有其他用法。在

现在您可以删除试图更正错误的其他行。在

相关问题 更多 >