0

I wrote some throw away code which takes a list of ids checks for duplicates and writes a list of ids. Nothing fancy just a small part of what I am working on..

I get this weird output. It looks to me like the delimiter is adding spaces where it shouldn't. Is delimiter just between words or line ? Very confused.

r s 9 3 6 4 5 5 4
r s 9 3 1 1 1 7 1 
r s 7 8 9 0 2 0 2 5 
r s 7 6 5 2 3 3 1 
r s 7 2 1 0 4 8 
r s 6 9 8 3 2 6 7 
r s 6 4 6 5 6 5 7
r s 6 2 9 2 4 2 
r s 6 1 9 9 1 1 5 6

Code:

__author__ = 'prumac'
import csv

allsnps = []

def open_file():
    ifile  = open('mirnaduplicates.csv', "rb")
    print "open file"
    return csv.reader(ifile)

def write_file():
    with open('mirnaduplicatesremoved.csv', 'w') as fp:
        a = csv.writer(fp, delimiter=' ')
        a.writerows(allsnps)


def checksnp(name):
    if name in allsnps:
        pass
    else:
        allsnps.append(name)

def mymain():
    reader = open_file()
    for r in reader:
        checksnp(r[0])
    print len(allsnps)
    print allsnps
    write_file()

mymain()
prussiap
  • 667
  • 1
  • 7
  • 14

1 Answers1

3

.writerows() expects a list of lists. Instead, you are handing it a list of strings, and these are treated as sequences of characters.

Put each string in a tuple or list:

a.writerows([val] for val in allsnps)

Note that you could do this all a little more efficiently:

with open('mirnaduplicates.csv', "rb") as ifile, \
     open('mirnaduplicatesremoved.csv', 'wb') as fp:
    reader = csv.reader(ifile)
    writer = csv.writer(fp, delimiter=' ')

    seen = set()
    seen_add = seen.add
    writer.writerows(row for row in reader if row[0] not in seen and not seen_add(row[0]))
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343