我有超過 500 個格式相同的 csv 檔案 - 6 列(計數、高度、鏈接、標題、標題、寬度)和多行。我想只保留這些 >500 個 csv 檔案中的“鏈接”列(包含所有 url)。最后,我想用所有這些資料鏈接將最終檔案轉換為 .txt 檔案。
原始樣本 csv 檔案 -
count,height,link,title,titles,width
1,142,https://url.jpg,,,338
..
...
....
到 .txt 檔案 -
https://url.jpg
https://url.jpg
https://url.jpg
https://url.jpg
https://url.jpg
這里有人有解決方案嗎?
非常感謝。
uj5u.com熱心網友回復:
您可以使用 pandas 將CSV檔案作為資料框讀取,從每個檔案中獲取鏈接列,然后將結果寫入文本檔案。嘗試這個 :
from os.path import abspath, join
from os import listdir
import pandas as pd
abs_path = abspath(path) # path of your folder
for filename in listdir(abs_path):
df = pd.read_csv(join(abs_path, filename), usecols=['link'])
df.to_csv(filename '.txt', header=None, index=None, sep=' ', mode='w')
注意:列名(至少鏈接列)必須明確存在于所有檔案中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/436211.html
