0

i have lot of files which are not formatted properly.

The data is something like this

select *                  from table1 \n , table2
  select cnt(1) from                          table3

How can i remove unnecessary blank spaces and "/n"

result

select * from table1,table2
select cnt(1) from table3

Thanks in advance

nnnnmmm
  • 233
  • 2
  • 6
  • 17
  • Is it ​​​​​​​​​​​​​​​`\n` or `/n`? – Remi Guan Jun 13 '16 at 06:43
  • it is \n and not /n – nnnnmmm Jun 13 '16 at 06:44
  • 5
    Possible duplicate http://stackoverflow.com/questions/1546226/a-simple-way-to-remove-multiple-spaces-in-a-string-in-python and http://stackoverflow.com/questions/8270092/python-remove-all-whitespace-in-a-string – Doron Cohen Jun 13 '16 at 06:45
  • I dont want to do on string but on entire file. – nnnnmmm Jun 13 '16 at 06:50
  • You have to read the file, and then you get a string. after removing the spaces just write the results back to the file. – Doron Cohen Jun 13 '16 at 06:52
  • Isn't this quite different from the other questions mentioned by Doron? The OP doesn't want some of the spaces to be retained. For example, spaces around commas. – gaganso Jun 13 '16 at 06:56

2 Answers2

1

I have not tested this but i think the idea will work for any number of spaces before a comma and not just one like the previous answer which was more simple but not as practical. For every line in the file we look at every word of that line, but we make an empty list for for each line that will be fixed. if the word in the line is a " " or "\n" we do not append it. then we go through the new list and print out the word plus a space if it not equal to a comma.

 for line in file:
        wordl = []
        strng = ""
        for word in line:
            if word != " " or word != "\n":
                wordl.append(word)
        for w in wordl:
            if w != ",":
                strng += w + " "
            else:
                strng += w
    print strng
0

The easiest way to do this is by:

s = 'select *                  from table1 \n    ,     table2'
result = " ".join(s.split()).replace(' , ', ',')
print result

Output: select * from table1,table2

Gal Dreiman
  • 3,969
  • 2
  • 21
  • 40