简体   繁体   English

从单个CSV列表中下载多个CSV文件(Python)

[英]Download multiple CSV files from a list in a single CSV (Python)

I have a 2 column CSV with download links in the first column and company symbols in the second column. 我有两列CSV,第一列带有下载链接,第二列有公司符号。 For example: 例如:

http://data.com/data001.csv , BHP http://data.com/data001.csv

http://data.com/data001.csv , TSA http://data.com/data001.csv

I am trying to loop through the list so that Python opens each CSV via the download link and saves it separately as the company name. 我试图遍历该列表,以便Python通过下载链接打开每个CSV并将其分别保存为公司名称。 Therefore each file should be downloaded and saved as follows: 因此,应按以下方式下载和保存每个文件:

BHP.csv BHP.csv

TSA.csv TSA.csv

Below is the code I am using. 下面是我正在使用的代码。 It currently exports the entire CSV into a single row tabbed format, then loops back and does it again and again in an infinite loop. 当前,它将整个CSV导出为单行选项卡格式,然后循环返回并一次又一次地无限循环。

import pandas as pd

data = pd.read_csv('download_links.csv', names=['download', 'symbol'])
file = pd.DataFrame()
cache = []

for d in data.download:
    df = pd.read_csv(d,index_col=None, header=0)
    cache.append(df)
    file = pd.DataFrame(cache)
    for s in data.symbol:
        file.to_csv(s+'.csv')

print("done")

Up until I convert the list 'cache' into the DataFrame 'file' to export it, the data is formatted perfectly. 直到我将列表“缓存”转换为DataFrame“文件”以将其导出之前,数据的格式都已完美格式化。 It's only when it gets converted to a DataFrame when the trouble starts. 仅在故障开始时将其转换为DataFrame时。

I'd love some help on this one as I've been stuck on it for a few hours. 我已经坚持了几个小时,希望对此有所帮助。

Iterate over both fields in parallel: 并行遍历两个字段:

for download, symbol in data.itertuples(index=False):
    df = pd.read_csv(d,index_col=None, header=0)  
    df.to_csv('{}.csv'.format(symbol))
import pandas as pd
data = pd.read_csv('download_links.csv')
links = data.download
file_names = data.symbol
for link, file_name in zip(links,file_names):
    file = pd.read_csv(link).to_csv(file_name+'.csv', index=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM