我有一列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)更多推荐
发布评论