使用Tweepy,我正在使用python写入csv文件,并且标头每隔一行重复一次
x=0
x+=1
with open('NAME' + str(x) + '.csv', 'w' , newline='') as f:
for user in tweepy.Cursor(api.followers, screen_name="Name").items(5):
thewriter = csv.writer(f)
thewriter.writerow(['Username', 'location'])
thewriter = csv.writer(f)
thewriter.writerow([user.screen_name , user.location])
答案 0 :(得分:0)
您的脚本应更改为此:
x=0
x+=1
with open('NAME' + str(x) + '.csv', 'w' , newline='') as f:
thewriter = csv.writer(f)
thewriter.writerow(['Username', 'location'])
for user in tweepy.Cursor(api.followers, screen_name="Name").items(5):
thewriter.writerow([user.screen_name , user.location])
您只需要创建thewriter
对象一次,当然,您只想创建一次标题,而不是您看到的每隔一行。将内容从循环遍历的for循环中移出即可。