2

I am trying to fetch data from a file but data is not coming properly because no. of spaces are different at different places.

Ex: file have following below given data. here every word have more than one space(Note this is not string, its a file data and i m using readlines())

08:30:34  Lane   2  Typ   1  Prt  1  Tid     2  Amt       4.99  Mode   0  Tndr   1  Oper       130  Tran        74  ID  53062261  Log Dt 2014/08/05  Log Tm 08:30:34  LaneType 1  HasPrinter 1 

I want output like this :

08:30:34 Lane 2 Typ 1 Prt 1 Tid 2 Amt 4.99 Mode 0 Tndr 1 Oper 130 Tran 74 ID 53062261 Log Dt 2014/08/05 Log Tm 08:30:34 LaneType 1 HasPrinter 1.

Thanks

Dadep
  • 2,702
  • 5
  • 23
  • 37
  • 1
    Possible duplicate of [Replacing all multispaces with single spaces](https://stackoverflow.com/questions/27022052/replacing-all-multispaces-with-single-spaces) – tk421 Sep 14 '17 at 19:18

2 Answers2

2

you can use regex :

>>> s="08:30:34  Lane   2  Typ   1  Prt  1  Tid     2  Amt       4.99  Mode   0  Tndr   1  Oper       130  Tran        74  ID  53062261  Log Dt 2014/08/05  Log Tm 08:30:34  LaneType 1  HasPrinter 1  "
>>> import re
>>> re.sub(' +',' ',s)
'08:30:34 Lane 2 Typ 1 Prt 1 Tid 2 Amt 4.99 Mode 0 Tndr 1 Oper 130 Tran 74 ID 53062261 Log Dt 2014/08/05 Log Tm 08:30:34 LaneType 1 HasPrinter 1 '

so if you want to keep a list from readlines() :

>>> f = open('yourfile.txt','r')
>>> result=[re.sub(' +',' ',i) for i in f.readlines()]
Dadep
  • 2,702
  • 5
  • 23
  • 37
  • thank you reply , I am new in python and i tried this but it is working only for string. i am using below code f = open( sys.argv[1], 'r') lines = f.readlines() f.close() that data is written in file and i am reading that file and i want to remove extra space from file .. in each line – Subodh Gupta Sep 14 '17 at 18:39
  • `readlines()` return a list of each line of your file, you can iterate through the list and apply the regex method (see Edit) – Dadep Sep 14 '17 at 19:02
  • i need one more , i want to write a code where a function take two file as a input and compare both file data and return out if any mismatch found in both file data. once again thanks for help – Subodh Gupta Sep 15 '17 at 09:57
  • This is an other question, please open other question is you do not find already the answer (I think you'll find here : https://stackoverflow.com/questions/19120489/compare-two-files-report-difference-in-python). – Dadep Sep 15 '17 at 11:14
1

Try this:

s = 'aaa bb    ccccc    d f    ggggg'
s = ' '.join(s.split())

If you are reading from a file with readline():

with open('bar.txt', 'r') as f:
    while True:
        s = f.readline()
        s = ' '.join(s.split())
        if not s:
            break
        print(s)
prometeu
  • 579
  • 6
  • 21
  • thank you reply , I am new in python and i tried this but it is working only for string. i am using below code f = open( sys.argv[1], 'r') lines = f.readlines() f.close() that data is written in file and i am reading that file and i want to remove extra space from file .. in each line .. – Subodh Gupta Sep 14 '17 at 18:42