python 3.x - Reading and writing text files -


i have work text file so:

<keyplat> = 1228.3 <keytran> = 690.3 <keytotal> = 1918.6 <kavplat> = 2996.3 <kavtran> = 3811.4 <kavtotal> = 6807.7 

i'm attempting use data write new text file expected output this:

table 1 platform    1228.3 transfers   690.3  table 2 platform    1918.6 transfers   2996.3 total       6807.7 

i trying pull numbers out , store them write new file. method using search key characters , when start pulling numbers. i'm not sure go point though.

def foo():       # open write , read files.       f1 = open('text.txt', 'r')       f2 = open('to.txt', 'w')         f2.write("table 1"\n)       f2.write("platform\t")       #start of digit information       searchquery = '='       #end of digits       searchquery2 = '<'       line in f1:             letter in line:                   if letter not isdigit():                    if letter == searchquery:                    if letter == searchquery2: 

rather checking character character suggest using keyword in on line. can use split generate list of words between whitespaces instance ["<keyplat>", "=", "1228.3"] outcome of "<keyplat> = 1228.3".split(' '). , need format output prefer. prefer .format notation.

i using lines short cut. should instead use file.

lines = ["<keyplat> = 1228.3", "<keytran> = 690.3", "<keytotal> = 1918.6"]  f1 = open("testtable.txt", "w")  line in lines:     values = line.split(' ')     if "keyplat" in values[0]:         print("{0:10s}{1:10s}".format("platform", values[2]), file=f1)     elif "keytran" in values[0]:         print("{0:10s}{1:10s}".format("transfers", values[2]), file=f1)     elif "keytotal" in values[0]:         print("{0:10s}{1:10s}".format("total", values[2]), file=f1)  f1.close() 

output:

platform  1228.3     transfer  690.3      total     1918.6