Skip to main content
 首页 » 编程设计

python之创建 .csv 文件时出现 UnicodeEncodeError

2025年05月04日19bonelee

我正在尝试创建一个 .csv 文件,其中包含我从 Twitter 搜索 API 存储到列表中的数据。我已经使用我选择的关键字(在本例中为“reddit”)保存了最后 100 条推文,并且我试图将每条推文保存到 .csv 文件中的一个单元格中。我的代码如下,我返回的错误是:

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

如果有人知道我能做些什么来解决这个问题,将不胜感激!

import sys 
import os 
 
 
import urllib 
import urllib2 
import json 
from pprint import pprint 
import csv 
 
import sentiment_analyzer 
 
import codecs 
 
class Twitter: 
    def __init__(self): 
        self.api_url = {} 
        self.api_url['search'] = 'http://search.twitter.com/search.json?' 
 
    def search(self, params): 
 
        url = self.make_url(params, apitype='search') 
        data = json.loads(urllib2.urlopen(url).read().decode('utf-8').encode('ascii',     'ignore')) 
 
        txt = [] 
        for obj in data['results']: 
            txt.append(obj['text']) 
 
        return '\n'.join(txt) 
 
    def make_url(self, params, apitype='search'): 
 
 
        baseurl = self.api_url[apitype]  
        return baseurl + urllib.urlencode(params) 
 
 
if __name__ == '__main__': 
    try: 
        query = sys.argv[1] 
    except IndexError: 
        query = 'reddit' 
 
    t = Twitter() 
 
    s = sentiment_analyzer.SentimentAnalyzer() 
 
    params = {'q': query, 'result_type': 'recent', 'rpp': 100} 
 
    urlName = t.make_url(params) 
    print urlName 
    txt = t.search(params) 
 
    print s.analyze_text(txt) 
 
    myfile = open('reddit.csv', 'wb') 
    wr = csv.writer(myfile, quoting=csv.QUOTE_MINIMAL) 
    wr.writerow(txt) 

请您参考如下方法:

来自 csv 的 Python 2 文档模块:

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 文件,使用 Python 的内置 Unicode 字符串支持没有太多困难——还有这个 answer .