我已将Reddit数据提取到Python,并打算将其写入csv / txt文件。问题是我只能将100行中的一行写入文件。其余的由于某种原因不会被写入文件。
我对Python非常业余,如果有人可以帮助我解决我做错的事情,我将不胜感激。非常感谢!
代码:
import praw
myreddit = praw.Reddit(client_id='xxxxx', client_secret='xxxxxx',
user_agent='Testing app')
newposts = myreddit.subreddit('Bitcoin').new(limit=100) #connection to 100 newest subreddits
for post in newposts:
data = ('ID: {}, Date_utc: {}, Upvotes: {}, Number of comments: {}, Subthread name: {}'.format(post,
post.created_utc,
post.ups,
post.num_comments,
post.title)) #fetch following data: post ID, time, up votes, amt of comments, the titel of the subreddit
with open('C:\\testRedditFetch.csv','w') as f: #open the file
f.write(data) #write the file
答案 0 :(得分:1)
您每次都要写入一个csv,这会覆盖当前数据,您可以将“ w”更改为“ a”,这是附加的,这样您的数据将被附加而不被覆盖。
import praw
import csv
myreddit = praw.Reddit(client_id='xxxxx', client_secret='xxxxxx',
user_agent='Testing app')
newposts = myreddit.subreddit('Bitcoin').new(limit=100) #connection to 100 newest subreddits
with open('C:\\testRedditFetch.csv','a') as f:
headers = ['ID', 'Date_utc', 'Upvotes', 'Number of Comments', 'Subthread name']
writer = csv.DictWriter(f, fieldnames=headers, extrasaction='ignore', dialect='excel')
writer.writeheader()
for post in newposts:
data = {'ID: {}, Date_utc: {}, Upvotes: {}, Number of comments: {}, Subthread name: {}'.format(post,
post.created_utc,
post.ups,
post.num_comments,
post.title)} #fetch following data: post ID, time, up votes, amt of comments, the titel of the subreddit
writer.writerow(data)