开发者

writing values generated from different functions in a single file

开发者 https://www.devze.com 2023-03-24 19:48 出处:网络
I have written this code and what I am trying to do here is to write the values generated from this function and another one which are in stored as lists in timeList and

I have written this code and what I am trying to do here is to write the values generated from this function and another one which are in stored as lists in timeList and parameterList. I am not exactly supposed to write the values of timeList in the same function I guess. It should probably be done in a separate function. I have already got the timeList in curve.dat file. Now I have to write the parameterList in the same file but not following the values of timeList but on the right side of the timeList. The timeList in curve.dat file is:

0.07

0.06

0.08

0.12

0.11

0.09

0.14

0.05

0.06

The parameterList is:

[0.744527502029805, 1.3466145472841764, 2.8186875392157371, 3.2822440320192392, 7.9272007790523782, 6.0493081375991276, 9.2609232787439613, -611.06135600582172, -399.75236270058838]

Now I should have the contents of curve.dat file as the following:

0.07       0.744527502029805  
0.06       1.3466145472841764  
0.08       2.8186875392157371  
0.12       3.2822440320192392  
0.11       7.9272007790523782  
0.09       6.0493081375991276  
0.14       9.2609232787439613  
0.05       -611.06135600582172  
0.06       -399.75236270058838  

Could someone help with this please. Thank you.

def time(transcriptionFile) :
    with open("transcriptions.txt", "r") as tFile :
        timeList = []
        parameterList = []

        for line in tFile :
            li = line.split()
            if li :
      开发者_如何转开发          start_time = (int(li[0]) / 10000000.)
                end_time = (int(li[1]) / 10000000.) 
                duration = ((int(li[1]) -int(li[0]))/10000000.)
                timeList.append(duration) # t(u) values for plotting the bezier curves

                with open("curve.dat", "w") as outFile:
                    outFile.write("\n".join(str(x) for x in timeList))

                poly = poly_coeff(start_time, end_time, duration)

                Newton(poly, 0.42, parameterList) 


Do not write timeList to the file while inside the for line in tFile loop. Notice how many times you are opening and closing the file (very inefficient). Also notice if you open the file in 'w' (write) mode instead of 'a' (append) mode, it overwrites the previous contents...

Instead, wait (delay gratification!) until you have both timeList and parameterList assembled. Then do

with open("curve.dat", "w") as outFile:
    for t,p in zip(timeList,parameterList):
        outFile.write('{t}\t{p}\n'.format(t=t,p=p))
0

精彩评论

暂无评论...
验证码 换一张
取 消