2

hi wanna to remove leading and trailing spaces in csv files

24333,   116,    47,MCD,00000000000000000017996,   112
24333,   116,    47,MCD,00000000000000610036485,   112  
24333,   116,    47,MCD,00000000000000610036485,   112

can any one help the code i tried

import csv

csvfile= open('strip.csv','r')
csvfile1= open('strip11.csv','w') 
stripped = (row.strip() for row in csvfile)
reader = csv.reader(stripped,delimiter=' ')
writer= csv.writer(csvfile1)
for row in reader:
   writer.writerow(row)
martineau
  • 119,623
  • 25
  • 170
  • 301
prasanna.1024kt
  • 39
  • 1
  • 1
  • 5
  • `writer.writerow([e.strip() for e in row])` – Bob Dylan Jan 18 '16 at 15:15
  • Possible duplicate of [How to trim whitespace (including tabs)?](http://stackoverflow.com/questions/1185524/how-to-trim-whitespace-including-tabs) – gpinkas Jan 18 '16 at 15:28
  • added the above code m getting the output as comma are added in the spcaes part below"24333,",,,"116,",,,,"47,MCD,00000000000000000017996,",,,112 "24333,",,,"116,",,,,"47,MCD,00000000000000610036485,",,,112 "24333,",,,"116,",,,,"47,MCD,00000000000000610036485,",,,112 – prasanna.1024kt Jan 18 '16 at 16:36
  • i chaged the code has below, stripping hwppening perfectly but new empty row is appending after each row in csv file . can any one help below is my code import csv csvfile= open('strip.csv','r') reader = csv.reader(csvfile) next(reader, None) csvfile1= open('strip4.csv','w') stripped = (row.strip() for row in csvfile) reader = csv.reader(stripped,delimiter=',') writer= csv.writer(csvfile1) for row in reader: writer.writerow([e.strip() for e in row]) – prasanna.1024kt Jan 18 '16 at 16:48

1 Answers1

7

The magic comes from applying strip on each item in each row record.

Stripping a string is done usually like " abc ".strip. To refer to the strip method without having actual string at hand, one can import string and then use string.strip.

The map(string.strip, list_of_strings_to_strip) applies the strip to each item in the record and returns them in a list.

>>> import string
>>> rec = ["  a  ", "  b  ", "  c  "]
>>> map(string.strip, rec)
["a", "b", "c"]

The complete working example for your data:

import csv
import string

with open("data.csv") as f:
    reader = csv.reader(f, delimiter=",")
    with open("stripped.csv", "w") as fo:
        writer = csv.writer(fo)
        for rec in reader:
            writer.writerow(map(string.strip, rec))

The with open(... are so called context managers ensuring, that the created file descriptor will get closed regardless of possible failure during the inner block execution.

Resulting file looks like:

24333,116,47,MCD,00000000000000000017996,112
24333,116,47,MCD,00000000000000610036485,112
24333,116,47,MCD,00000000000000610036485,112
Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
  • i used above code also still empty row is coming can any one help on he same .Why m getting empty row after each row having data – prasanna.1024kt Jan 19 '16 at 05:51
  • @prasanna.1024kt Make sure, you copy paste my complete example. It is likely, you mixed your and mine solution, and in your solution you iterate about file lines (which include newline at the end) and mine iterates about csv records (which have the new line stripped for reading and also manage new lines properly when writing). Note, that this newline stripping is done by `csv.reader` and `csv.writer` and has nothing to do with `string.strip`. – Jan Vlcinsky Jan 19 '16 at 19:40