View Full Version : Python: getting rid of spaces

September 7th, 2009, 10:42 PM
8 : L = open('gdprank.csv').read()

for line in L.splitlines():
clear_line = [word.strip() for word in line.split()]
print " ".join(clear_line)

This code gets rid of some spaces in a file. But it doesn't actually change the file. When I put in "print L" I still get the old file with a lot of spaces. How do I atleast save the new file without the spaces?


September 7th, 2009, 11:22 PM
you never write out to the file, you are only changing the data in memory.

something like (untested)

linesin = open('gdprank.csv').readlines()
fileout = open('gdprank.csv', 'w')

for line in linesin:
clear_line = [word.strip() for word in line.split()]
fileout.write(" ".join(clear_line) + '\n')

September 8th, 2009, 02:09 PM
just a thought -- is this a part of a larger program, or is the only purpose of this script to remove extra spaces from a file? if the latter, then an easier way to do it (without needing a scripting language) would be to use tr

tr -s ' ' < gdprank.csv > gdprank.new && mv gdprank.{new,csv}

the -s parameter to tr squeezes out repeat occurences of a character.