file - Writing tabs in a specific way (Not from lists) in python -
i new python , went through many posts regarding question ask. if duplicate, please feel free put on hold.
question : write file tabs , multiple values (not list), used following snipet
for line in open("dat.zmine"): if "zavg" in line: char = line.split()[0] value = line.split()[1] fidzavg.write(str(stepval*filenum)) fidzavg.write("\t") fidzavg.write(value) fidzavg.write("\n")
where fidzavg file id . looks bit tedious opposed in c++
printf(fidzavg,"%g\t%g\n", stepval*filenum, value)
can reduce python snippet simular put c++?
any appreciated.
you can same thing in python:
fidzavg.write("%g\t%g\n" % (stepval*filenum, float(value)))
or use str.format
:
fidzavg.write("{0:g}\t{1:g}\n".format(stepval*filenum, float(value)))
note assignment line
can simplified using slicing , tuple (un)packing:
char, value = line.split()[:2]
Comments
Post a Comment