在 for 循环中只写入一次值到文件

Writing value just once to file in for loop

我有这个迭代:

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 一次,并在一行中加上标题行中的每个相应值。

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")