我是python的新手,我正在寻找一些帮助来创建一个存储来自两个不同变量的数据的循环。此方法仅保存第一条推文。
# Twitter Loop
for tweet in tweepy.Cursor(api.search,q=search+
" -filter:retweets",
result_type='recent',
lang="en").items(num_tweets):
raw = tweet.text
# Text processing
clean = re.sub(r"(?:@\S*|#\S*|http(?=.*://)\S*)", "", raw)
result = cool.api(clean)
# CSV File
import csv
f = open("file.csv", "a")
c = csv.writer(f)
c.writerow([clean, result])
f.close()
我做了很多尝试,而且我不确定这样做的正确方法。我应该为每个变量创建一个循环吗?
答案 0 :(得分:1)
简单。您需要将调用移至for循环中的csv.writerow()
。这样,你迭代的每条推文都会被写入csv文件(或者更确切地说,是从推文中获取的数据):
import csv
f = open("file.csv", "a", newline="")
c = csv.writer(f)
for tweet in tweepy.Cursor(api.search,q=search+
" -filter:retweets",
result_type='recent',
lang="en").items(num_tweets):
raw = tweet.text
clean = re.sub(r"(?:@\S*|#\S*|http(?=.*://)\S*)", "", raw)
result = cool.api(clean)
c.writerow([clean, result]) # Write each tweet to the csv file.
f.close()
使用上下文管理器语句with
可以改进代码。这意味着无论发生什么情况,您的文件将始终关闭:
import csv
with open('file.csv', 'a') as file:
c = csv.writer(file)
for tweet in tweepy.Cursor(api.search,q=search+
" -filter:retweets",
result_type='recent',
lang="en").items(num_tweets):
raw = tweet.text
clean = re.sub(r"(?:@\S*|#\S*|http(?=.*://)\S*)", "", raw)
result = cool.api(clean)
c.writerow([clean, result])