简体   繁体   English

如果多个大文本文件太大而无法单独转换,如何将它们转换为一个 CSV 文件?

[英]How do I convert several large text files into one CSV file if they are too large to be converted individually?

I have several large .text files that I want to consolidate into one .csv file.我有几个大的 .text 文件,我想将它们合并为一个 .csv 文件。 However, each of the files is to large to import into Excel on its own, let alone all together.但是,每个文件都很大,无法单独导入 Excel,更不用说全部导入了。

I want to create a use pandas to analyze the data, but don't know how to get the files all in one place.我想创建一个使用熊猫来分析数据,但不知道如何在一个地方获取所有文件。

How would I go about reading the data directly into Python, or into Excel for a .csv file?我将如何将数据直接读入 Python 或 Excel 中的 .csv 文件?

The data in question is the 2019-2020 Contributions by individuals file on the FEC's website.有问题的数据是个人在 FEC 网站上提交的2019-2020 年贡献

You can convert each of the files to csv and the concatenate them to fom one final csv file您可以将每个文件转换为 csv 并将它们连接起来形成一个最终的 csv 文件

import pandas as pd
csv_path = 'pathtonewcsvfolder'                    # use your path
all_files=os.listdir("path/to/textfiles")
x=0
for filename in all_files:
    df = pd.read_fwf(filename)
    df.to_csv(os.path.join(csv_path,'log'+str(x)+'.csv'))
    x+=1
all_csv_files = glob.iglob(os.path.join(csv_path, "*.csv"))

converted_df=pd.concat((pd.read_csv(f) for f in all_csv_files), ignore_index=True)
converted_df.to_csv('converted.csv')

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

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