将许多.txt文件转换为csv并将其组合 [英] Converting many .txt files into csv and combining them

查看:492
本文介绍了将许多.txt文件转换为csv并将其组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多.txt文件.我想将一些以特定名称结尾的文件转换为csv,并将它们组合为一个csv.

I have many .txt files. I want to convert a few files ending with specific names into csv and combine them into one csv.

### Folder Name: text_files
python_gramm.py
aadd01.txt
aaxx02.txt
aaff03.txt
hhdd01.txt
attd02.txt
auud03.txt
.
.

一个文本文件包含如下所示的数据列:

A text file contains columns of data as given below:

我只想将名称为"___ 01.txt"的那些.txt文件转换为csv格式并将其合并 我的代码

I would like to convert only those .txt files having name '___01.txt' to csv format and combine them My code

#### python_gramm.py 
import os
import glob
    os.chdir('text_files/')
    extension = 'txt'
    all_filenames = [i for i in glob.glob('*01.{}'.format(extension))]
    #combine all files in the list
    combined_csv = pd.concat([pd.read_csv(f, delimiter='\t') for f in all_filenames ])
    #export to csv
    combined_csv.to_csv( "combined_csv.csv", index=False, encoding='utf-8-sig')

以上代码成功运行,没有错误.

Above code ran successfully without errors.

print(combined_csv)
0      4.18890   9.325750  ...  2.438860e-05            4.100250e+04
1      4.21399   9.339870  ...  2.461170e-05            4.063090e+04
2      4.26399   9.341690  ...  2.451400e-05            4.079280e+04
3      4.80021   9.346420  ...  2.442850e-05            4.093570e+04
4      6.50219   9.339300  ...  2.432250e-05            4.111400e+04

推荐答案

您的代码说

glob.glob('1.{}'.format(extension))

但是我想你是说

glob.glob('*01.{}'.format(extension))

我认为您的全局找不到任何名为"1.txt"的文件.

I don't think your glob is finding any files named "1.txt".

这篇关于将许多.txt文件转换为csv并将其组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆