![](/img/trans.png)
[英]How do I get my python output into a csv or excel file?
[英]How do I export my python output to GoogleSheets or an Excel Doc? (Sheets Preferred)
我正在使用Twint模塊從Twitter獲取數據,我需要將輸出打印到GoogleSheet中,該如何處理?
import twint
from datetime import datetime, timedelta
three_days_ago = (datetime.now() - timedelta(3)).strftime('%Y-%m-%d')
# type keywords you want
keywords = ["Wawa"]
# loop for the keywords
for words in keywords:
# Configure
c = twint.Config()
c.Since = three_days_ago
c.Search = words
c.Format = "Tweet id: {id} | Tweet: {tweet} | Username: @{username} | Date: {date} | Time: {time} \n"
# Run
twint.run.Search(c)
我更新了您的代碼。 根據Twint文檔,您可以將輸出導出到.csv文件中,然后再將.csv導入GoogleSheets中。
import twint
from datetime import datetime, timedelta
three_days_ago = (datetime.now() - timedelta(3)).strftime('%Y-%m-%d')
# type keywords you want
keywords = ["Wawa"]
# loop for the keywords
for words in keywords:
# Configure
c = twint.Config()
c.Since = three_days_ago
c.Search = words
c.Format = "Tweet id: {id} | Tweet: {tweet} | Username: @{username} | Date: {date} | Time: {time} \n"
# add this lines to your script
c.Store_csv = True
# here you can filter the needed columns (optional)
c.Custom_csv = ["id", "user_id", "username", "tweet"]
# here name your output
c.Output = "tweets_csv"
twint.run.Search(c)
使用熊貓
import pandas as pd
df = pd.DataFrame(data)
df.to_xls(path_to_file_with_name.xlsx)
它應該保存您的excel
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.