Unicode 字符到土耳其字符
Unicode characters to Turkish characters
(编辑:我原来的问题贴在这里,但问题已经解决,下面的代码是正确的)。我正在寻找有关如何将 Unicode 字符转换为土耳其语字符的建议。以下代码(在线发布)为单个用户抓取推文并输出一个 csv 文件,但土耳其语字符以 Unicode 字符形式出现,即 \xc4。我在 mac.
上使用 Python 3
import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
reload(sys)
sys.setdefaultencoding(default_encoding)
import tweepy #https://github.com/tweepy/tweepy
import csv
import string
import print
#Twitter API credentials
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""
def get_all_tweets(screen_name):
#Twitter only allows access to a users most recent 3240 tweets with this method
#authorize twitter, initialize tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
#initialize a list to hold all the tweepy Tweets
alltweets = []
#make initial request for most recent tweets (200 is the maximum allowed count)
new_tweets = api.user_timeline(screen_name = screen_name,count=200)
#save most recent tweets
alltweets.extend(new_tweets)
#save the id of the oldest tweet less one
oldest = alltweets[-1].id - 1
#keep grabbing tweets until there are no tweets left to grab
while len(new_tweets) > 0:
#print "getting tweets before %s" % (oldest)
#all subsiquent requests use the max_id param to prevent duplicates
new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest)
#save most recent tweets
alltweets.extend(new_tweets)
#update the id of the oldest tweet less one
oldest = alltweets[-1].id - 1
将 tweepy 推文转换为将填充 csv 的二维数组
outtweets = [[tweet.id_str, tweet.created_at, tweet.text)] for tweet in alltweets]
写入 csv
with open('%s_tweets.csv', 'w', newline='', encoding='utf-8-sig') as f:
writer = csv.writer(f)
writer.writerow(["id","created_at","text"])
writer.writerows(outtweets)
pass
if __name__ == '__main__':
输入你要下载的账号的用户名
get_all_tweets("")
csv
module docs建议您在打开文件时指定编码。 (并且您还使用 newline=''
以便 CSV 模块可以对换行符进行自己的处理)。写入行时不要对 Unicode 字符串进行编码。
import csv
with open('test.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['id','created_at','text'])
writer.writerows([[123, 456, 'Äβç']])
(编辑:我原来的问题贴在这里,但问题已经解决,下面的代码是正确的)。我正在寻找有关如何将 Unicode 字符转换为土耳其语字符的建议。以下代码(在线发布)为单个用户抓取推文并输出一个 csv 文件,但土耳其语字符以 Unicode 字符形式出现,即 \xc4。我在 mac.
上使用 Python 3import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
reload(sys)
sys.setdefaultencoding(default_encoding)
import tweepy #https://github.com/tweepy/tweepy
import csv
import string
import print
#Twitter API credentials
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""
def get_all_tweets(screen_name):
#Twitter only allows access to a users most recent 3240 tweets with this method
#authorize twitter, initialize tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
#initialize a list to hold all the tweepy Tweets
alltweets = []
#make initial request for most recent tweets (200 is the maximum allowed count)
new_tweets = api.user_timeline(screen_name = screen_name,count=200)
#save most recent tweets
alltweets.extend(new_tweets)
#save the id of the oldest tweet less one
oldest = alltweets[-1].id - 1
#keep grabbing tweets until there are no tweets left to grab
while len(new_tweets) > 0:
#print "getting tweets before %s" % (oldest)
#all subsiquent requests use the max_id param to prevent duplicates
new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest)
#save most recent tweets
alltweets.extend(new_tweets)
#update the id of the oldest tweet less one
oldest = alltweets[-1].id - 1
将 tweepy 推文转换为将填充 csv 的二维数组
outtweets = [[tweet.id_str, tweet.created_at, tweet.text)] for tweet in alltweets]
写入 csv
with open('%s_tweets.csv', 'w', newline='', encoding='utf-8-sig') as f:
writer = csv.writer(f)
writer.writerow(["id","created_at","text"])
writer.writerows(outtweets)
pass
if __name__ == '__main__':
输入你要下载的账号的用户名
get_all_tweets("")
csv
module docs建议您在打开文件时指定编码。 (并且您还使用 newline=''
以便 CSV 模块可以对换行符进行自己的处理)。写入行时不要对 Unicode 字符串进行编码。
import csv
with open('test.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['id','created_at','text'])
writer.writerows([[123, 456, 'Äβç']])