csv writer在每个单词中添加分隔符..

时间:2013-05-20 22:21:04

标签: python csv

我写了一些抛弃代码,它带有一个id重复列表检查并写入一个id列表。没有什么比我正在做的事情只有一小部分......

我得到了这个奇怪的输出。在我看来,分隔符是在不应该添加空格的地方。单词或行之间是分隔符吗?非常困惑。

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

代码:

__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()

1 个答案:

答案 0 :(得分:3)

.writerows()需要列表清单。相反,您正在处理一个字符串列表,这些字符串被视为字符序列。

将每个字符串放在元组或列表中:

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

请注意,您可以更高效地完成此任务:

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]))