1

We are creating a subfile from a big csv file. The subfile contains only those rows which have their first column's value as D1:

import csv
with open('input.csv', 'rb') as csvfile:

     reader = csv.reader(csvfile, delimiter='|', quotechar='|')

     writer1 = csv.writer(open('output.csv', 'w'), delimiter = ' ')

     for row in reader:
         a = row[0]
         row  =  '|'.join(row)
         if (a=='D1'):
                 writer1.writerow(row)

This code gives 2 issues:

  1. A Blank line comes after every row in new csv file
  2. Every word has extra spaces between it's letters. So, "Hello" becomes "H e l l o".
6
  • 1
    Check for automatic newline conversion for open() (which OS are you on?). Provide sample data and ouput. Commented Aug 19, 2016 at 8:22
  • See also (not a duplicate): stackoverflow.com/questions/29840849/… Commented Aug 19, 2016 at 8:23
  • The extra spaces sounds like you are using a string as a sequence (like a list or a tuple). Commented Aug 19, 2016 at 8:26
  • 1
    why are you doing row = '|'.join(row) ? you could just set delimiter = '|' in the writer and write the row directly Commented Aug 19, 2016 at 8:30
  • Thanks Pedru. This has solved the issue where "Hello" was becoming "H e l l o". Now just need to remove blank lines that are coming after every row in the new csv file. Commented Aug 19, 2016 at 8:33

2 Answers 2

1

This code runs fine:

import csv
with open('input.csv', 'rb') as csvfile:

     reader = csv.reader(csvfile, delimiter='|', quotechar='|')

     writer1 = csv.writer(open('output.csv', 'wb'), delimiter = '|')

     for row in reader:
         a = row[0]
         if (a=='D1'):
                 writer1.writerow(row)

Thanks to Pedru

Sign up to request clarification or add additional context in comments.

Comments

0

From python csv docs..

import csv
with open('eggs.csv', 'w', newline='') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=' ',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])

So it seems that You have to use lists ['Spam'] with spamwriter.writerow... If its not list, this method writerow is creating its own list from iterable input like this:

>>> a = 'ala ma kota'
>>> list(a)
['a', 'l', 'a', ' ', 'm', 'a', ' ', 'k', 'o', 't', 'a'] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.