簡體   English   中英

在 CSV 文件中獲取輸出

[英]Get Output in CSV-File

此代碼向我輸出與 Google 新聞中的關鍵字“航空公司”相關的新聞項目,這些新聞項目在一天內發布。 在這里,我得到了標題、描述和 URL。 我有一個問題:我想在 CSV 文件中以表格形式從我的控制台輸出結果,任何人都可以幫助我嗎?

非常感謝!

import ssl
ssl._create_default_https_context = ssl._create_unverified_context

from GoogleNews import GoogleNews
import csv

googlenews = GoogleNews()

googlenews.set_encode('utf_8')
googlenews.set_lang('de')
googlenews.set_period('1d')
googlenews.get_news('Airlines')

print(googlenews.results())
´´´

googlenews.results()中的數據寫入 csv 的一種方法可能是使用pathlib完成的, pathlib所示:

from pathlib import Path

Path('data.csv').write_text(googlenews.results())

或者在純 Python 中使用上下文管理器:

with open('data.csv', 'w') as f:
    f.write(googlenews.results())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM