将数据写入csv文件时出现编码错误

时间:2017-06-04 16:07:44

标签: python python-2.7 python-3.x

from tweetpy import *
import re
import json
from pprint import pprint
import csv

# Import the necessary methods from "twitter" library
from twitter import Twitter, OAuth, TwitterHTTPError, TwitterStream

# Variables that contains the user credentials to access Twitter API
ACCESS_TOKEN =  ''
ACCESS_SECRET = ''
CONSUMER_KEY = ''
CONSUMER_SECRET = ''

oauth = OAuth(ACCESS_TOKEN, ACCESS_SECRET, CONSUMER_KEY, CONSUMER_SECRET)

# Initiate the connection to Twitter Streaming API
twitter_stream = TwitterStream(auth=oauth)

# Get a sample of the public data following through Twitter
iterator = twitter_stream.statuses.filter(track="#kindle",language="en",replies="all")
 # Print each tweet in the stream to the screen

 # Here we set it to stop after getting 10000000 tweets.
 # You don't have to set it to stop, but can continue running
 # the Twitter API to collect data for days or even longer.

tweet_count = 10000000

file = "C:\\Users\\WELCOME\\Desktop\\twitterfeeds.csv"
with open(file,"w") as csvfile:
    fieldnames=['Username','Tweet','Timezone','Timestamp','Location']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    for tweet in iterator:
        #pprint(tweet)
        username = str(tweet['user']['screen_name'])
        tweet_text = str(tweet['text'])
        user_timezone = str(tweet['user']['time_zone'])
        tweet_timestamp=str(tweet['created_at'])
        user_location = str(tweet['user']['location'])
        print tweet
        tweet_count -= 1
        writer.writerow({'Username':username,'Tweet':tweet_text,'Timezone':user_timezone,'Location':user_location,'Timestamp':tweet_timestamp})

        if tweet_count <= 0:
            break

我正在尝试使用列'username''Tweet''Timezone''Location''Timestamp'向csv文件发送推文。

但是我收到以下错误:

tweet_text = str(tweet['text'])
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in position 139: ordinal not in range(128).

我知道这是编码问题,但我不知道要编码的变量的确切位置。

2 个答案:

答案 0 :(得分:1)

  1. 使用Python 3,因为Python 2 setup.py模块没有很好地编码。
  2. csvopenencoding选项一起使用。
  3. 删除newline转换(在Python 3中str已经是Unicode字符串。
  4. 结果:

    str

    如果使用Python 2,请获取第三方with open(file,"w",encoding='utf8',newline='') as csvfile: fieldnames=['Username','Tweet','Timezone','Timestamp','Location'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() for tweet in iterator: username = tweet['user']['screen_name'] tweet_text = tweet['text'] user_timezone = tweet['user']['time_zone'] tweet_timestamp = tweet['created_at'] user_location = tweet['user']['location'] . . . 模块以克服unicodecsv个缺点。

答案 1 :(得分:0)

如果您真的想要转换所有的unicode数据

/\${([^}]+)}/g