我正在将我的代码的输出写入.csv文件。有三个目录,每个目录包含50个文件。我想将每个目录文件的输出写入不同的列。喜欢;在第一列之后写入csv文件的第二列
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
..
我想写第一组50列,第二组50列,第三组最后50列
最终输出的样子,
group1 group2 group3
file1 145 89 87
file2 850 100 47
file3 650 120 67
file4 230 140 97
你真的应该使用['with'(https://stackoverflow.com/q/9282967/3901060)当您打开文件。 – FamousJameous
@ FamousJameous2可以用''来解决写入下一列的问题 – hassan
不可能在原地编辑文件,只能在最后写行。因此打开文件,将你想要的内容存储为一行,并将其写入'out'。更多信息:[这里](https://stackoverflow.com/questions/5453267/is-it-possible-to-modify-lines-in-a-file-in-place) –