Converting many .txt files into csv and combining them

2024/10/5 19:43:05

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: enter image description here

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 globos.chdir('text_files/')extension = 'txt'all_filenames = [i for i in glob.glob('*01.{}'.format(extension))]#combine all files in the listcombined_csv = pd.concat([pd.read_csv(f, delimiter='\t') for f in all_filenames ])#export to csvcombined_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
Answer

Your code says

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

But I think you mean

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

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

https://en.xdnf.cn/q/120315.html

Related Q&A

Calculation between two columns in Python?

When I tried to do some calculation between two columns (like division), I get an error: column_ratio[x]=(float(column1[y]))/(float(column2[z])) TypeError: tuple indices must be integers, not str. C…

Why does input() always return a string?

Here is my code:age = input("How old are you?: ") print (age) print (type(age))Result:How old are you?: 3535class str <<--- This is problem!But, If I use.. age = int(input("How …

Windowed mode cannot run

Why does pyinstaller exe not run in windowed mode but fine without it? I have changed over to a windows OS from Linux. Never had any issue before hand, how do I correct this.

inserting a variable into an fstring using .replace()

I have a code something similar to bellow. name = Dave message = f<name> is a really great guy! message = message.replace(<name>, {name}) print(message)the variables are a little more compl…

How to allow caps in this input box program for pygame?

I found this input box module on the internet but it only allows lower case no upper. So could someone tell me what to change in the module to allow caps as im creating a small multiplayer game and i n…

Why testing error rate increases at high values of K in KNN algorithm?

I am getting the error rates like this up to 20 values what might be the reason for this ?k_values: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Error [0.0, 0.0, 0.0, 0.0, 0…

Divide and Conquer. Find the majority of element in array

I am working on a python algorithm to find the most frequent element in the list. def GetFrequency(a, element): return sum([1 for x in a if x == element])def GetMajorityElement(a):n = len(a)if n == …

Scraping dynamic webpage using Python

I am trying to scrape following dynamically generated webpage https://www.governmentjobs.com/careers/capecoral?page=1 Ive used requests, scrapy, scrapy-splash but I simply get page source code and I d…

numba cuda deprecation error : how to update my code?

Im running a jupyter notebook frome here : https://github.com/noahgift/nuclear_powered_command_line_tools/blob/master/notebooks/numba-cuda.ipynb The docs of current numba/cuda is here : https://numba.r…

reverse nested dicts using python

I already referred these posts here, here and here. I have a sample dict like as shown below t = {thisdict:{"brand": "Ford","model": "Mustang","year": …