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".
Mazdak
  • 105,000
  • 18
  • 159
  • 188
Aditya
  • 615
  • 3
  • 12
  • 26
  • 1
    Check for `automatic newline conversion` for `open()` (which OS are you on?). Provide sample data and ouput. – handle Aug 19 '16 at 08:22
  • See also (not a duplicate): http://stackoverflow.com/questions/29840849/writing-a-csv-file-in-python-that-works-for-both-python-2-7-and-python-3-3-in/29841468#29841468 – cdarke Aug 19 '16 at 08:23
  • The extra spaces sounds like you are using a string as a sequence (like a list or a tuple). – cdarke Aug 19 '16 at 08:26
  • 1
    why are you doing `row = '|'.join(row)` ? you could just set `delimiter = '|'` in the writer and write the row directly – Pedru Aug 19 '16 at 08: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. – Aditya Aug 19 '16 at 08:33
  • The blank lines were removed after changing the write mode from "w" to "wb". Thanks everyone – Aditya Aug 19 '16 at 08:37

2 Answers2

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

Aditya
  • 615
  • 3
  • 12
  • 26
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'] 
yourstruly
  • 972
  • 1
  • 9
  • 17