-1

I coded how to load and save txt file using pandas in python.


import glob

filenames = sorted(glob.glob("D:/a/test*.txt"))
filenames = filenames[0:5]

import numpy as np
import pandas as pd

for f in filenames:
  df = pd.read_csv(f, skiprows=[1,2,3], dtype=str, delim_whitespace=True) 
  df.to_csv(f'{f[:-4]}.csv', index=False)

 ------>There are 10 result files in a folder 
 - test1.txt, test2.txt, test3.txt, test4.txt, test5.txt, 
 test1.csv, test2.csv, test3.csv, test4.csv, test5.csv

#Result csv.file(test1.csv)

abc:,1.233e-03
1.234e-04,
1.235e-02,
1.236e-05,
1.237e-02,
1.238e-02,

But I have some problems as follows.

  1. I don't know how to rename test1.txt, test2.txt, test3.txt, test4.txt, test5.txt into c1.csv, c2.csv, c3.csv, c4.csv, c5.csv.

  2. I want remove 'abc:,'data in all test(1,2,3,4,5).csv files, but I don't know how to delete and replace data.

Do you know how to rename(change) file name and remove data (specific character) referred to above problems in python?

origin data
test1.txt (it is similar to other file-{test2,test3,test4, test5}.txt)
abc:  1.233e-03
def:  1.64305155216978164e+02
ghi: 4831
jkl:
 1.234e-04
 1.235e-02
 1.236e-05
 1.237e-02
 1.238e-02

Expected result
(it must chage test1,2,3,4,5.txt files into c1,2,3,4,5.csv files, it only remove herder name(abc:), also def:,ghi:,jkl: rows should remove.)
 1.233e-03
 1.234e-04
 1.235e-02
 1.236e-05
 1.237e-02
 1.238e-02
platanus
  • 49
  • 7
  • Does this answer your question? [How to move a file?](https://stackoverflow.com/questions/8858008/how-to-move-a-file) – sushanth Jun 09 '20 at 04:41

2 Answers2

0

Here is what you can do:

import os
import glob
import pandas as pd


for f in glob.glob("*.txt"):
    df = pd.read_csv(f, skiprows=[1,2,3], dtype=str, delim_whitespace=True)
    df = df.replace('abc:,','')
    os.rename(f,f'{f[:-4]}.csv')
    df.to_csv(f'{f[:-4]}.csv', index=False)
Red
  • 26,798
  • 7
  • 36
  • 58
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/215669/discussion-on-answer-by-ann-zen-renamechange-file-name-and-remove-data-specif). – Samuel Liew Jun 10 '20 at 13:14
0
  1. You can rename your file using os.rename() method or you can create a new file using f= open("file_name.extension","w+") and write the output to new file.

  2. You can use the replace() method once you load your data into a string variable.

Prathamesh
  • 1,064
  • 1
  • 6
  • 16
  • -I want to use code like 'df.read_csv' and 'to_csv'. Do you know how to solve different type? I already replace()-replace('abc:,', ''). but it also occurrs the error. – platanus Jun 09 '20 at 05:20