在第一列之后写入第二列csv文件

时间:2017-06-29 13:49:14

标签: python python-2.7 csv

我正在将代码的输出写入.csv文件。每个目录有三个目录包含50个文件。我想在不同的列中写入每个目录文件的输出。等;

       group1  group2 group3
file1 1445      89    87
file2 1225     100    47
file3 650      120    67
file4 230      140    97

我有以下代码,

from collections import Counter
import glob
import os

out= open( 'output.csv','a')
out.write (';''group-1')
out.write (';''group-2')
out.write (';''group-3')
out.write('\n')
i = 1

while i<=50:
 out.write( "file-%d" %i )
 out.write('\n')
 i+=1
i=1
path = 'group/group-*-files/*.txt'

files=sorted(glob.glob(path))
c=Counter()

for filename in files:

 for line in open(filename,'r'):

    c.update(line.split())

 for item in c.items():
  oi=("{}\t{}".format(*item))  
  out_array = oi.split()

  if out_array[0]=='00000000':

   out.write(out_array[1])
   out.write('\n')
  c.clear()

我遇到的问题并没有解决,答案开始写在文件号50后的第一栏

file48
file49
file50
1445
1225
.. 

我想在group1列下编写前50个答案,在group2中编写下50个答案,在group3中编写最后50个答案

最终输出看起来像,

group1  group2 group3
file1 145      89     87
file2 850      100    47
file3 650      120    67
file4 230      140    97

2 个答案:

答案 0 :(得分:0)

这就是我重写代码的方法。我所做的改变是:

  • 打开文件时使用with语句以确保它们已关闭
  • 使用csv模块更轻松地编写csv文件
  • 在将其写入文件之前,一次构建一行,立即写入整行。

由于我并不真正了解您文件中的内容,因此未经过全面测试。

import csv
from collections import Counter
import glob
import os

with open( 'output.csv','a') as out:
    writer =csv.writer(out, delimiter='\t')
    writer.writerow(['']+['group{}'.format(i) for i in range(1, 4)])
    path = 'group/group-*-files/*.txt'

    files=sorted(glob.glob(path))
    c=Counter()
    for i, filename in enumerate(files):
        line = ['file-{}'.format(i)]
        with open(filename) as infile:
            for line in infile:
                c.update(line.split())
        for key, count in c.items():
            if key == '00000000':
                line.append(count)
        writer.writerow(line)
        c.clear()

答案 1 :(得分:0)

错误的缩进至少有一个问题。首先通过以下方式生成所有文件名:

...
while i<=50:
 out.write( "file-%d" %i )
 out.write('\n')                # replace \n to column delimiter \t
 i+=1

然后开始处理文件。您删除此行i=1,所有其他文字必须以与out.write相同的缩进开始

from collections import Counter
import glob
import os

out= open( 'output.csv','a')  # flag a - Do you want append to existing file ?
out.write('file;group-1;group2;group3') # You forget column 1 - filename
# out.write (';''group-1')
# out.write (';''group-2')
# out.write (';''group-3')
# out.write('\n')
i = 1
while i<=50:
 out.write( "file-%d" %i )
 # out.write('\n')
 out.write(';')    # Insert character for column delimiter
 i+=1
 # i=1  Delete, because will cause infinite loop
 # Following code must run inside while loop, indent to the same level
 # as previous lines
 path = 'group/group-*-files/*.txt'

 files=sorted(glob.glob(path))
 c=Counter()

 for filename in files:

  for line in open(filename,'r'):

     c.update(line.split())

  for item in c.items():
   oi=("{}\t{}".format(*item))  
   out_array = oi.split()

   if out_array[0]=='00000000':

    out.write(out_array[1])
    # out.write('\n') - You don want create new lines, but only new columns for every group
    out.write(';') 

   c.clear()
  out.write('\n') # New line - new record