Kimi Shui
Kimi Shui

Reputation: 93

remove the unwanted columns in data

I have 500 txt files in a folder data example:

1-1,0-7,10.1023,
1-2,7-8,/,
1-3,8-9,A,
1-4,9-10,:,
1-5,10-23,1020940830716,

I would like to delete the last "," in each line. to :

1-1,0-7,10.1023
1-2,7-8,/
1-3,8-9,A
1-4,9-10,:
1-5,10-23,1020940830716

How do I do that with a for loop to delete them from all of 500 files?

Upvotes: 0

Views: 130

Answers (2)

David Erickson
David Erickson

Reputation: 16683

I usually do something like this.

  1. Change the folder_path variable
  2. Change the filename_pattern variable. This is just extra in case you have specific file patterns in your folder that you want to consider. You can simply set this variable to (blank) if irrelevant.

Also, the * takes anything that matches the pattern i.e. Book1, Book2, etc. Before running the code print(files) to make sure you have all of the correct files. I am not sure if :

import glob
import os
import pandas

#read files in
folder_path = 'Documents'
filename_pattern = 'Book'
files = glob.glob(f'{folder_path}//{filename_pattern}*.txt')
df = (pd.concat([pd.read_csv(f, header=None)
              .assign(filename=os.path.basename(f)) 
              for f in files]))


#read files out
for file, data in df.groupby('filename'):
    data.iloc[:,:-2].to_csv(f'{folder_path}/{file}', 
                            index=False, 
                            header=False)

Upvotes: 1

U13-Forward
U13-Forward

Reputation: 71610

Try using this code:

for fname in filenames:
    with open(fname, 'r') as f:
        string = f.read().replace(',\n','\n')
    with open(fname, 'w') as w:
        w.write(string)
        

Upvotes: 2

Related Questions