How to do Data cleaning with multiple text files

Viewed 72

I have directory with log files. So, for reading and concatenating i'm using following commands:

filenames = glob('*.log')
df = [pd.read_csv(f) for f in filenames

Every log files looks like this:

Tracer: (1) 18F-Nb25    Batch no: 3459  Date: 2020-01- 3


Time       IS current   IS volt.    Dee RF  Magnet  Probe   Coll-l  Foil Target Coll-r  Vacuum
05:25:39    0   0   0.0 0.0 130.85  0.1 0.1 0.0 0.1 0.1 2.2E-06
05:25:40    0   0   0.0 0.0 130.85  0.1 0.1 0.0 0.1 0.1 2.2E-06
05:25:41    0   0   0.0 0.0 130.85  0.1 0.1 0.0 0.1 0.1 2.2E-06
05:25:42    0   0   0.0 0.0 130.85  0.1 0.1 0.0 0.1 0.1 4.2E-06

For data cleaning and transformation, I'm using:

fline=open("abc.csv", ).readline().rstrip()

Output:

'Tracer: (1) 18F-Nb25 \tBatch no: 3451 \tDate: 2020-01- 2,Tracer: (1) 18F-Nb25 \tBatch no: 3452 \tDate: 2020-01- 2,Tracer: (1) 18F-Nb25 \tBatch no: 3453 \tDate: 2020-01- 2,Tracer: (1) 18F-Nb25 \tBatch no: 3454 \tDate: 2020-01- 2,Tracer: (1) 18F-Nb25 \tBatch no: 3455 \tDate: 2020-01- 2,Tracer: (1) 18F-Nb25 \tBatch no: 3456 \tDate: 2020-01- 3,Tracer: (1) 18F-Nb25 \tBatch no: 3457 \tDate: 2020-01- 3,Tracer: (1) 18F-Nb25 \tBatch no: 3458 \tDate: 2020-01- 3'

after that, for splitting:

fline = fline.split('\t')

and finally:

df = pd.read_csv('abc.csv',  sep='\t', skiprows=[0,1,2,3], names=['Time','IS current','IS volt.','Dee','RF','Magnet','Probe','Coll-l','Foil','Target','Coll-r', 'Vacuum'])

df['Date'] = fline[2].replace("Date: ","")
df['Batch'] = fline[1].replace("Batch no: ","") 

But Date and Batch no shown in csv as per first log file read..... need help to show date and batch number in correct way.

Any help would be much appreciated. Thanks in advance

0 Answers
Related