1

将 csv 文件与另一个文件合并时,我无法正确格式化它。我有以下代码:

list_dir = glob.glob('C:/.../*')
imprint = 'C:/.../imprint_report.csv'
export_dir = 'C:/.../Export//'

for imprint_d in csv.DictReader(open(imprint)):
    for list_file in list_dir:
        list_token = os.path.basename(list_file)[:6]
        if imprint_d['token'] == list_token:
            v = open(list_file, 'rb')
            r = csv.reader(v, delimiter='\t')
            row0 = r.next()
            row0.append('token')
            all_rows = []
            for item in r:
                item.append(imprint_d['token'])
                all_rows.append(item)
                #print item
            output = open(export_dir+list_token+'_append.csv', 'wb')
            writer = csv.writer(output, lineterminator=',')
            writer.writerows(all_rows)

成功合并了我需要的数据,但是格式不正确。而不是将附加数据设置为每一行并保持格式,我要附加的数据list_file在哪里:x

col1    col2    col3    col4
  a       b       c       x
  d       e       f       x
  g       h       i       x

我将所有数据合并到一行中,如下所示:

a    b    c    x    d    e    f    x    g    h    i    x

关于我在哪里出错的任何想法?谢谢!

4

1 回答 1

2

lineterminator=','也许改变?

于 2013-11-12T19:52:12.900 回答