Sapna Sharma
Sapna Sharma

Reputation: 754

CSV reader putting /n after each row

I have generated a CSV file from excel. I am trying to read this CSV file using python CSV. However after each row I get /n. How to remove this /n.

Here is my code:

with open('/users/ps/downloads/test.csv','rU') as csvfile
      spamreader = csv.reader(csvfile,dialect=csv.excel_tab)
      a = [] 
      for row in csvfile:
        a.append(row)
 print a

I get result like this:

['HEADER\n', 'a\n', 'b\n', 'c\n', 'd\n', 'e']

I want to have results like this:

['HEADER', 'a', 'b', 'c', 'd', 'e']

Upvotes: 0

Views: 42

Answers (2)

t..
t..

Reputation: 1101

you could try a replace

a.replace('\n','')

edit:
working verison- a.append(row.replace('\n',''))

Upvotes: 1

Nishant Nawarkhede
Nishant Nawarkhede

Reputation: 8400

You can use strip

x = ['HEADER\n', 'a\n', 'b\n', 'c\n', 'd\n', 'e']

In [6]: def f(word):
   ...:     return word.strip()
   ...:

In [7]: map(f, x)
Out[7]: ['HEADER', 'a', 'b', 'c', 'd', 'e']

In [8]:

Upvotes: 1

Related Questions