Search code examples
python-2.7nested-loopsstreamwriteroverwrite

python write 40 times loops results in text


I wanna loop the following scripts for 40 times ,then write it into a txt file.However, I am the begginning user of Python, no idea how to do that,

print ('TIME           =            0.00 hours since 1999-06-30 14:00:00 +00:00')
print ('x_spw_eye      =     138.50')
print ('y_spw_eye      =       7.20')
print ('pdrop_spw_eye =         1000.00')

for i in range(1500):
    print " ".join('%8.1f' % 0 for j in range(16))
    print " ".join('%8.1f' % 0 for j in range(16))
    print " ".join('%8.1f' % 0 for j in range(4))

and also should change the space number of the parameter 'x_spw_eye', 'y_spw_eye','pdrop_spw_eye',

For example,

in the next loops , the head should be like,

TIME           =            6.00 hours since 1999-06-30 14:00:00 +00:00
x_spw_eye      =     138.10
y_spw_eye      =      12.50
pdrop_spw_eye =          985.00

Is there any idea what kind of clue should I follow with?

Thank you so much!!!

All the bests, Li


Solution

  • I got another script that meet the original requirement better, finally could write the results to a txt file,that is,

    f = open ('test_2.txt','w')
    for x in range (41):
        f.write ('TIME           =            0.00 hours since 1999-06-20 14:00:00 +00:00\n')
        f.write ('x_spw_eye      =     138.50\n')
        f.write ('y_spw_eye      =       7.20\n')
        f.write ('pdrop_spw_eye =         1000.00\n')
    
    
        for i in range(1500):
    
            for j in range (16):
                f.write ('%8.1f'% (0)),
            f.write ('%8.1f' % (0)+'\n')
            for j in range (16):    
                f.write ('%8.1f' % (0)),
            f.write ('%8.1f' % (0)+'\n')
            for j in range (4):
                f.write ('%8.1f' % (0))  
            f.write ('%8.1f' % (0)+'\n')
    
    
    f.close()
    

    This one is better than last one, which could write all the results into a txt file, otherwise this results really a huge file