只写一次值来存档for循环

时间:2015-05-18 08:43:59

标签: python iteration file-writing

我有这个迭代:

with open("myFile.txt", "r") as landuse:
    next(landuse)
    for j in landuse:
        landuseList = j.split(";")
        clcKlasse = landuseList[2].strip()
        landusePlz = landuseList[3].strip("\"")
        landuseArea = landuseList[6].strip()
        landuseAreaFloat = float(landuseArea.replace("," , "."))
        if landusePlz in dictPlz:
            areaPlz = dictPlz.get(landusePlz)
            relativeShare = (landuseAreaFloat * 100) / areaPlz
            nf.write(str(clcKlasse) + "\t" + str(relativeShare) + "\t")
            prevAreaPlz = areaPlz
    print "Done"

我需要在我的文件(nf)中使用此结构:

PLZ    "abc"    "def"    "ghi"    "jkl"    "mnl"    "opq"
1       7.54     1.20    9.98     19.57     8.68    2.15

PLZ     "abc"
2       10.17     

...

那就是我从中读取的文件:

"CLCKlasse";"PLZ";"area"
"abc";"1";7.54
"def";"1";1.20
"ghi";"1";9.98   
"jkl";"1";19.57
"mnl";"1";8.68
"opq";"1";2.15
"abc";"2";10.17

...

如您所见,每行与plz相关。但是,我需要plz仅向nf写一次,每行中的每个对应值加上标题。

1 个答案:

答案 0 :(得分:1)

from operator import itemgetter
from itertools import groupby


#input file
f=open('mytxt','rb')
#output file
f_out=open('out','w')

#skip the first line
header=f.readline()

# read every line
lines=f.readlines()
lines=[i.split(';')  for i in lines if i != '\n']

#grouping
groups=[]
for k,g in groupby(lines,itemgetter(1)):
    groups.append(list(g))


#iterate and write to a file
for j in range(len(groups)):
    headers=[[i[0],i[2]]  for i in groups[j]]
    final_headers=["PLZ"+'\t'] + [i[0]+'\t' for i in headers]
    final_values=[str(j+1)+'\t']+[i[1].strip()+'\t' for i in headers]
    f_out.write("".join(final_headers))
    f_out.write("\n")
    f_out.write("".join(final_values))
    f_out.write("\n")