gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:09:22 25 4
gpt4 key购买 nike

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'“位置”“时间戳”

但我收到以下错误:

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

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

最佳答案

  1. 使用 Python 3,因为 Python 2 csv 模块不能很好地进行编码。
  2. openencodingnewline 选项结合使用。
  3. 删除 str 转换(在 Python 3 中 str 已经是 Unicode 字符串。

结果:

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']
.
.
.

如果使用 Python 2,请获取第 3 方 unicodecsv 模块来克服 csv 缺点。

关于python - 将数据写入 csv 文件时出现编码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44356057/

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