Hi everyone ,
I had to read from a file and for each data between delimiter i need to remove the white space and i have written the following program in jython
When i am trying to rewrite ,its rewriting at the end of source file.
filesrc = open('c:/FILE/split_doc.txt','r+')
for list in filesrc.readlines():
#split the records by the delimiter
fields = list.split(',')
list = ",".join([s.strip() for s in fields])
filesrc.writelines(list+"\n")
filesrc.close()
So i did some modification and added file.seek so I can rewrite on the source lines and it worked to some extend except it was adding two extra lines at the end which means some issue with seek part.
The modified program is
filesrc = open('c:/ODI_FILE/split_doc.txt','r+')
lines=0
for list in filesrc.readlines():
#split the records by the delimiter
fields = list.split(',')
list = ",".join([s.strip() for s in fields])
filesrc.seek(lines)
filesrc.writelines(list+"\n")
lines += len(list+"\n")
filesrc.close()
Please help me with the correct logic.
The correct source file with extra white spaces
52 ,William ,Kudo ,28/03/199300:00:00
11,Andrew, Andersen,22/02/199900:00:00
12,John ,Galagers,20/04/200000:00:00
13,Jeffrey ,Jeferson,10/06/198800:00:00
20,Jennie,Daumesnil,28/02/198800:00:00
21,Steve,Barrot,24/09/199200:00:00
22,Mary,Carlin,14/03/199500:00:00
30,Paul,Moore,11/03/199900:00:00
This is my wrong output
52,William,Kudo,28/03/199300:00:00
11,Andrew,Andersen,22/02/199900:00:00
12,John,Galagers,20/04/200000:00:00
13,Jeffrey,Jeferson,10/06/198800:00:00
20,Jennie,Daumesnil,28/02/198800:00:00
21,Steve,Barrot,24/09/199200:00:00
22,Mary,Carlin,14/03/199500:00:00
30,Paul,Moore,11/03/199900:00:00
9500:00:00
30,Paul,Moore,11/03/199900:00:00
here the last two lines should not have come
Please suggest the required and faster way as this is a sample file and i would have to have make this program work for millions of rows.
Is there is way to make this logic work with while loop too ?