从单个CSV(Python)列表中下载多个CSV文件(Download multiple CSV files from a list in a single CSV (Python))

编程入门 行业动态 更新时间:2024-10-26 18:18:46
从单个CSV(Python)列表中下载多个CSV文件(Download multiple CSV files from a list in a single CSV (Python))

我有一列2列CSV,第一列有下载链接,第二列有公司符号。 例如:

http://data.com/data001.csv ,必和必拓

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

我试图遍历列表,以便Python通过下载链接打开每个CSV文件,并将其另存为公司名称。 因此每个文件应该被下载并保存如下:

BHP.csv

TSA.csv

以下是我正在使用的代码。 它目前将整个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")

直到我将列表“缓存”转换为DataFrame的“文件”才能导出,数据格式完美。 只有当故障开始时它才转换为DataFrame。

因为我已经停留了几个小时,所以我很想在这个帮助中找到一些帮助。

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.

最满意答案

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) 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)

更多推荐

本文发布于:2023-07-18 19:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1164477.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   文件   列表中   Python   CSV

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!