简体   繁体   中英

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. For example:

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

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

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. Therefore each file should be downloaded and saved as follows:

BHP.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.

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. It's only when it gets converted to a DataFrame when the trouble starts.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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