如何使用tweepy在CSV文件中保存python熊猫数据框

时间:2018-09-24 14:43:21

标签: python pandas

我正在使用tweepy提取推文,并且正在保存熊猫数据帧,但无法将数据帧保存到我正在indent error的csv文件中。我的数据使用乌尔都语

有人可以帮我吗?

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)
df = pd.DataFrame(columns=['text'])
msgs = []
msg =[]
for tweet in tweepy.Cursor(api.search, q="اردو", rpp=1000).items(1000):
    msg = [tweet.text] 
    msg = tuple(msg)                    
    msgs.append(msg)
    df = pd.DataFrame(msgs)

    with open('ae.csv', 'w', newline='', encoding='utf-8') as csv_file:
    writer = csv.writer(csv_file, delimiter=';')
    writer.writerow(df)

我收到此错误

File "<ipython-input-43-c3cc5aa57112>", line 24
    with open('ae.csv', 'w', newline='', encoding='utf-8') as csv_file:
    ^
IndentationError: unexpected indent

1 个答案:

答案 0 :(得分:0)

Pandas具有内置的csv编写器。使用:

df.to_csv('ae.csv', sep = ';', line_terminator = '', encoding = 'utf-8')

有关更多详细信息,请查看文档:{​​{3}}