[Tutor] Space the final frontier!
John Corry
john.corry at ntlworld.com
Tue Apr 4 23:33:18 CEST 2006
Dear All,
I am having difficulty removing white spaces from my file. The file is 999
lines long and looks like the sample below:
001, new field,dial= 028 90 79 0154, dial=
002, borfiled, dial= 02890 618521, dial=
003, newcomp, dial=02890419689, dial=
The program, I am using to import the file does not like the spaces around
the numbers. The number should look like the "dial=02890419689" in the
third line. Thus the sample above should look like:
001,newfield,dial=02890790154,dial=
002,borfiled,dial=02890618521,dial=
003,newcomp,dial=02890419689,dial=
I have searched the tutor mailbag already and have picked up some good tips
on join, split and re but I can't seem to get it to work.
I am using the following code.
filename = "c:/test.txt"
import string
import os
import re
listy = []
input = open( filename, 'r') #read access
for line in input.readlines():
y = line
listy.append(y)
print listy
x = listy.pop()
re.sub(r'\s', '', x)
print y,x
del input
It produces the output:
['001, new field,dial= 028 90 79 0154, dial=\n']
001, new field,dial= 028 90 79 0154, dial=
001, new field,dial= 028 90 79 0154, dial=
['002, borfiled, dial= 02890 618521, dial=\n']
002, borfiled, dial= 02890 618521, dial=
002, borfiled, dial= 02890 618521, dial=
['003, newcomp, dial=02890419689, dial=']
003, newcomp, dial=02890419689, dial= 003, newcomp, dial=02890419689, dial=
Any help would be greatly appreciated.
Regards,
John.
More information about the Tutor
mailing list