You've put the initialization inside the for loop again, which is wrong. Look at my code and note that smallest and largest are before their respective loops.
20 Replies - 759 Views - Last Post: 09 January 2012 - 07:36 AM
Topic Sponsor:
#16
Re: Trying to convert a text file to python from fortran
Posted 06 January 2012 - 09:35 AM
#17
Re: Trying to convert a text file to python from fortran
Posted 06 January 2012 - 02:39 PM
Ok so i copied and pasted your code and it made no change, is there anything else you can thing of because I am at a loss here.
#18
Re: Trying to convert a text file to python from fortran
Posted 06 January 2012 - 02:41 PM
Without your data set I'm flying blind. Please post the file you are trying to parse along with your latest version of the code.
#19
Re: Trying to convert a text file to python from fortran
Posted 06 January 2012 - 02:51 PM
I attached the text file, and here is the most updated code:
import math
def get_data(f_obj,data_dict):
ret = []
for line in f_obj:
ret.append(line.split())
for val in ret:
for i in range(0,len(ret)):
data_dict[ret[i][0]]=ret[0][1:]
def compare_lrg(data_dict,index):
largest = float('-inf')
for key in data_dict.keys():
rxn_lrg = int(key)
if data_dict[key][index] == 'NaN':
continue
elif data_dict[key][index] > largest:
largest = float(data_dict[key][index])
rxn_lrg = int(key)
print largest, 'rxn:',rxn_lrg
def compare_sml(data_dict,index):
smallest = float('inf')
for key in data_dict.keys():
rxn_small = int(key)
if data_dict[key][index] == 'NaN':
continue
elif data_dict[key][index] < smallest:
smallest = float(data_dict[key][index])
rxn_small = int(key)
print smallest, 'rxn:',rxn_small
def main(file_str= 'reaction_results.txt'):
file_obj = open(file_str, 'r')
data_dict = {}
get_data(file_obj,data_dict)
file_obj.close()
print '*'*20
print 'Linear Interpolation data'
print '*'*20
print
print '*'*20
print 'Largest rms:'
compare_lrg(data_dict,1)
print '*'*20
print 'Smallest rms:'
compare_sml(data_dict,1)
print '*'*20
print 'Max error:'
compare_lrg(data_dict,2)
print '*'*20
print 'Largest T_max:'
compare_lrg(data_dict,3)
print '*'*20
print 'Largest R_max:'
compare_lrg(data_dict,4)
print '*'*20
print 'Largest Local max:'
compare_lrg(data_dict,5)
print '*'*20
print
print '*'*20
print 'Logarithmic Interpolation data'
print '*'*20
print
print '*'*20
print 'Largest rms:'
compare_lrg(data_dict,6)
print '*'*20
print 'Smallest rms:'
compare_sml(data_dict,6)
print '*'*20
print 'Max error:'
compare_lrg(data_dict,7)
print '*'*20
print 'Largest T_max:'
compare_lrg(data_dict,8)
print '*'*20
print 'Largest R_max:'
compare_lrg(data_dict,9)
print '*'*20
print 'Largest Local max:'
compare_lrg(data_dict,10)
print '*'*20
return data_dict
main()
Attached File(s)
-
reaction_results.txt (33.79K)
Number of downloads: 5
#20
Re: Trying to convert a text file to python from fortran
Posted 06 January 2012 - 05:36 PM
I have upvoted most of Motoma's posts here because it seems he is tirelessly struggling to fix someone else's newbie code, and deserves some kudos.
#21
Re: Trying to convert a text file to python from fortran
Posted 09 January 2012 - 07:36 AM
You had a problem with your get_data function, essentially, line 8 puts the first line's data in every element. Here is a proper function:
With that fixed, you now have to deal with all of the "-312"s in your data file.
def get_data(f_obj,data_dict):
for line in f_obj:
dat = line.split()
data_dict[dat[0]] = dat[1:]
With that fixed, you now have to deal with all of the "-312"s in your data file.
|
|

New Topic/Question
Reply





MultiQuote



|