![](/img/trans.png)
[英]Download multiple csv files from a web directory using Python and store them in disk, using as filename the anchor text
[英]Download multiple text files and keep them separate?
我正在嘗試下載多個 txt 文件,但不是將它們合並為一個文件,而是希望將它們分開並重命名 - 任何建議如何在 Python 中最好地做到這一點?
這是一個想法:
from gutenberg.acquire import load_etext
from gutenberg.cleanup import strip_headers
text_list=[161, 121, 105] #should be Jane Austen novels
with open(path + "directory", w):
for text in text_list:
text = strip_headers(load_etext(text)).strip()
save and rename them individually in the same directory
這是適用於合並文本文件的代碼:
with open(path + '/merged.txt', 'w') as f:
for text in text_list:
text = strip_headers(load_etext(text)).strip()
f.write(text)
我會交換循環和文件管理器,遍歷要保存的每個文本並使用唯一名稱將其寫入文件(我假設text_list
列表僅包含唯一文本名稱)。 path
應該以/
結尾,如果你想便攜,請改用os.path
。
from gutenberg.acquire import load_etext
from gutenberg.cleanup import strip_headers
text_list = [161, 121, 105]
path = "."
for text in text_list:
with open(f"{path}/{text}", "w") as f:
f.write(strip_headers(load_etext(text)).strip())
我只會做以下事情:
from gutenberg.acquire import load_etext
from gutenberg.cleanup import strip_headers
text_list = [fn1:"text_1", fn2:"text_2", fn3:"text_3"]
saved_files = {}
for text in text_list:
saved_file[text] = open(path + text,'w')
saved_file[text].write(strip_headers(load_etext(text_list[text])).strip())
saved_file[text].close()
編輯:刪除了不必要的導入
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.