UnicodeEncodeError:'ascii'编解码器无法对位置0中的字符u'\ xe4'进行编码:序数不在范围内(128)

时间:2020-12-08 20:21:19

I'm new to Python and have to build an application to get historical data from Twitter. I can see the tweets in my console and all the information I need! But the problem I have now is that I need to write this information to a .csv file but I encounter the following error while running the code: "UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 0: ordinal not in range(128)". I know that the problem is that the Tweets I'm collecting are written in Swedish so its a frequent use of the letter "ÅÄÖ". Can anyone please help me with this or have any pointers as to where i should start looking for a solution?

我是Python新手,必须构建一个应用程序来从Twitter获取历史数据。我可以在我的控制台中看到推文以及我需要的所有信息!但我现在的问题是我需要将此信息写入.csv文件,但在运行代码时遇到以下错误:“UnicodeEncodeError:'ascii'编解码器无法在位置0编码字符u'\ xe4' :序数不在范围内(128)“。我知道问题在于我收集的推文是用瑞典语写的,所以经常使用“ÅÄÖ”这个字母。任何人都可以帮助我这个或有任何指示,我应该从哪里开始寻找解决方案?

#!/usr/bin/python
# -*- coding: utf-8 -*-


from TwitterSearch import *
import csv

import codecs





try:
    tuo = TwitterUserOrder('Vismaspcs') # create a TwitterUserOrder


    ts = TwitterSearch(
        consumer_key = '',
        consumer_secret = '',
        access_token = '',
        access_token_secret = ''
    )

    # start asking Twitter about the timeline
    for tweet in ts.search_tweets_iterable(tuo):
        print( '@%s tweeted: %s' % ( tweet['user']['screen_name'], tweet['text']) )
        print (tweet['created_at'],tweet['favorite_count'],tweet ['retweet_count'])




    with open('visma.csv','w') as fout:
        writer=csv.writer(fout)    
        writer.writerows([tweet['user']['screen_name'],tweet['text'],tweet['created_at'],tweet['favorite_count'],tweet['retweet_count']])



except TwitterSearchException as e: # catch all those ugly errors
    print(e)

2 个解决方案

#1


The csv module cannot handle unicode in python2:

csv模块无法处理python2中的unicode:

Note This version of the csv module doesn't support Unicode input. Also, there are currently some issues regarding ASCII NUL characters. Accordingly, all input should be UTF-8 or printable ASCII to be safe; see the examples in section Examples.

注意此版本的csv模块不支持Unicode输入。此外,目前有一些关于ASCII NUL字符的问题。因此,所有输入应为UTF-8或可打印的ASCII以确保安全;请参阅示例部分中的示例。

You can use tweet['user']['screen_name'].encode("utf-8")...

你可以使用tweet ['user'] ['screen_name']。encode(“utf-8”)...

#2


Thank's, I use it with file.write(word['value'].encode("utf-8")) and it work too :)

谢谢,我用它与file.write(word ['value']。encode(“utf-8”)),它也工作:)

But you can try with file.encode('utf8') if it's not for write something

但是你可以尝试使用file.encode('utf8'),如果不是为了写东西的话

#1


The csv module cannot handle unicode in python2:

csv模块无法处理python2中的unicode:

Note This version of the csv module doesn't support Unicode input. Also, there are currently some issues regarding ASCII NUL characters. Accordingly, all input should be UTF-8 or printable ASCII to be safe; see the examples in section Examples.

注意此版本的csv模块不支持Unicode输入。此外,目前有一些关于ASCII NUL字符的问题。因此,所有输入应为UTF-8或可打印的ASCII以确保安全;请参阅示例部分中的示例。

You can use tweet['user']['screen_name'].encode("utf-8")...

你可以使用tweet ['user'] ['screen_name']。encode(“utf-8”)...

#2


Thank's, I use it with file.write(word['value'].encode("utf-8")) and it work too :)

谢谢,我用它与file.write(word ['value']。encode(“utf-8”)),它也工作:)

But you can try with file.encode('utf8') if it's not for write something

但是你可以尝试使用file.encode('utf8'),如果不是为了写东西的话