2012-05-10 BRAD 10
2012-05-08 BRAD 40
2012-05-08 BRAD 60
2012-05-12 TOM 100
我想要输出为
2012-05-08 BRAD|2|100
2012-05-10 BRAD| 1|10
2012-05-12 汤姆|1|100
我从这段代码开始::
import os,sys
fo=open("meawoo.txt","w")
f=open("test.txt","r")
fn=f.readlines()
f.close()
for line in fn:
line = line.strip()
sline = line.split("|")
p = sline[1].split(" ")[0],sline[2],sline[4]
print p
fo.writelines(str(p)+"\n")
fo.close()
o_read = open("meawoo.txt","r")
x_read=o_read.readlines()
from operator import itemgetter
x_read.sort(key=itemgetter(0))
from itertools import groupby
z = groupby(x_read, itemgetter(0))
print z
for elt, items in groupby(x_read, itemgetter(0)):
print elt, items
for i in items:
print i
It will be very helpful if u suggest me some usefull changes to my work.TIA