Trying to convert a text file to python from fortran

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 2368 Views - Last Post: 09 January 2012 - 07:36 AM Rate Topic: -----

#1 krajand14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-October 11

Trying to convert a text file to python from fortran

Posted 30 December 2011 - 06:13 PM

So I am trying to write a program that takes a file that is created using calculations and data in fortran then take the file that was created and use it to do some operations with the info in it in python. Problem is that the operations such as strip and do not seem to work on the text, seeing that all of the values in file are a string. I was wondering how to make strip work and also how I can group it so that the values in the string are grouped as their own value. For example here is some output below:
15000 nJpy85_to_zr86                        1.2944E+01  7.4604E+03  3.1916E+07 -8.5917E-66  1.1518E-69  1.9781E-04  9.3110E-02  3.2063E+07  2.1894E-69  2.4142E-69


Note: that this is just one value within the list so what happens now is when I say...
>>> print list1[0][0]
*it returns >>> 1

what I want it to return, though is 15000

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to convert a text file to python from fortran

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Trying to convert a text file to python from fortran

Posted 31 December 2011 - 09:12 AM

Please post the code where you get this data in to a variable.
Was This Post Helpful? 0
  • +
  • -

#3 krajand14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-October 11

Re: Trying to convert a text file to python from fortran

Posted 03 January 2012 - 11:47 AM

Okay so below is the code and to circumvent the problem i just pretty much parsed it, but I have a lot of data to do this with so I was hoping there was an easier fix.

def get_data(f_obj):
    import string
    list1 = []
    rxn_nam_list = []
    for line in f_obj:
        line = line.strip()
        line_list = line.split('\n')
        list1.extend(line_list)
    for i in range(0,len(list1)):
        react_list = list1[i].split(',')
        for word in react_list:
            s= word.find(' ')
            r = word.find(' ',s+1)
            if s:
                reaction_num = word[0:r]
                reaction_str = str(reaction_num)
                rxn_nam_list.append(reaction_str)
            react_list.insert(0,reaction_str)
    print list1

Was This Post Helpful? 0
  • +
  • -

#4 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Trying to convert a text file to python from fortran

Posted 03 January 2012 - 11:57 AM

How does this work for you?
def get_data(f_obj):
    ret = []
    for line in f_obj:
        ret.append(line.split())
    return ret

data_2d = get_data(open("myfile.dat"))
print data_2d[0][3] # print the fourth element of the first line


Was This Post Helpful? 1
  • +
  • -

#5 krajand14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-October 11

Re: Trying to convert a text file to python from fortran

Posted 04 January 2012 - 01:10 PM

That worked great I do have one problem though: so i made a dictionary with the numbers as the key, but I want to be able to compare the values in comparison to each key, so for example I want to compare the second value in the dictionary in all the keys to find which is the greatest. Here is the code I have:

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 rms_err(data_dict):
    for values in data_dict.values():
        for keys in data_dict.keys():
            rms_error = values[1]
            print rms_error
    
def main(file_str= 'reaction_results.txt'):
    file_obj = open(file_str, 'r')
    data_dict = {}
    get_data(file_obj,data_dict)
    file_obj.close()
    rms_err(data_dict)
    return data_dict

main()

Was This Post Helpful? 0
  • +
  • -

#6 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Trying to convert a text file to python from fortran

Posted 04 January 2012 - 01:44 PM

I think I understand what you mean:
largest = ()
for key in data_dict.keys():
    if data_dict[key][1] > largest[1]:
        largest = (key, largest[1])
print "The largest element was %s with a value of %s" % largest


This post has been edited by Motoma: 04 January 2012 - 01:45 PM

Was This Post Helpful? 2
  • +
  • -

#7 krajand14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-October 11

Re: Trying to convert a text file to python from fortran

Posted 04 January 2012 - 02:37 PM

It keeps returning tuple index out of range, which im not sure why
Was This Post Helpful? 0
  • +
  • -

#8 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Trying to convert a text file to python from fortran

Posted 04 January 2012 - 03:10 PM

You probably have an empty line. Do a print on the line before the error occurs to see why.
Was This Post Helpful? 1
  • +
  • -

#9 krajand14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-October 11

Re: Trying to convert a text file to python from fortran

Posted 04 January 2012 - 04:07 PM

I appreciate the help, I fixed that last problem, however after adding some of the code to make if more functional it is returning two error, one is saying that I reference the variable rxn_sml in the function rms_err_sml before assignment, but I do the same thing in the function rms_err_lrg and it works fine. And also when I run the function omitting the variable rxn_sml (just to get the output, the variable rxn_sml is not essential to running the program) it seems to not take into account the lowest values, intsead output the largest in the function rxn_err_sml:

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 rms_err_lrg(data_dict):
    for key in data_dict.keys():
        largest = float(data_dict[key][1])
        if data_dict[key][1] == 'NaN':
            continue
        elif data_dict[key][1] > largest:
            largest = float(data_dict[key][1])
            rxn_lrg = key
    print 'Largest RMS error: %f; rxn number: %s' %(largest,rxn_lrg)

def rms_err_sml(data_dict):
    for key in data_dict.keys():
        smallest = float(data_dict[key][1])
        if data_dict[key][1] == 'NaN':
            continue
        elif data_dict[key][1] < smallest:
            smallest = float(data_dict[key][1])
            rxn_sml = key
    print 'Smallest RMS error: %f; rxn number:%s' %(smallest,rxn_sml)
    
def main(file_str= 'reaction_results.txt'):
    file_obj = open(file_str, 'r')
    data_dict = {}
    get_data(file_obj,data_dict)
    file_obj.close()
    rms_err_lrg(data_dict)
    rms_err_sml(data_dict)
    return data_dict

main()


and this is the output with the omission:
Largest RMS error: 12.944000; rxn number: 15008
Smallest RMS error: 12.944000; rxn number:

Was This Post Helpful? 0
  • +
  • -

#10 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Trying to convert a text file to python from fortran

Posted 05 January 2012 - 07:41 AM

If you get through the entire data_dict but never run have data_dict[key][1] < smallest, rxn_sml will never be set.
Was This Post Helpful? 1
  • +
  • -

#11 krajand14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-October 11

Re: Trying to convert a text file to python from fortran

Posted 05 January 2012 - 05:39 PM

What do you mean never run? I call the function in main.
Was This Post Helpful? 0
  • +
  • -

#12 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Trying to convert a text file to python from fortran

Posted 05 January 2012 - 07:10 PM

Typo.
If you get through the entire data_dict but never have data_dict[key][1] < smallest, rxn_sml will never be set.

Likewise, with rms_err_lrg(), it works for the current data set, but isn't guaranteed to work with them all.
Was This Post Helpful? 1
  • +
  • -

#13 krajand14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-October 11

Re: Trying to convert a text file to python from fortran

Posted 05 January 2012 - 11:44 PM

Ok so it seems that the comparison is not working at that once the functions get to the end of the dictionary it is just assigning the largest or smallest variable names to the last value in the dictionary corresponding to the index specified in the function call. I am not sure why this is the case and why it isn't comparing correctly.
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):
    for key in data_dict.keys():
        largest = float(data_dict[key][index])
        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):
    for key in data_dict.keys():
        smallest = float(data_dict[key][index])
        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 '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
    print '*'*20
    print 'Logarithmic Interpolation data'
    print '*'*20
    print
    print 'Largest rms:'
    compare_lrg(data_dict,6)
    
    return data_dict

main()

Was This Post Helpful? 0
  • +
  • -

#14 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Trying to convert a text file to python from fortran

Posted 06 January 2012 - 07:43 AM

You overwriting your largest and smallest for each element in the dictionary.

Here is what you meant to do:

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



You'll see I've preinitialized smallest to infinity, and largest to negative infinity. This ensures that the first iteration of the loop will set those variables, regardless of the first element's value.
Was This Post Helpful? 1
  • +
  • -

#15 krajand14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-October 11

Re: Trying to convert a text file to python from fortran

Posted 06 January 2012 - 09:29 AM

Ok I see what you are saying, however, when I run it the rxn_name is always the same regardless of the value and this should not be the case. It should save rxn_small or rxn_lrg depending on if the value is larger than the initialized value. Not sure why this is happening because the key name is being save and updated in the elif suite with the test for greater than and also the same thing with the less than. Also the function that tests the smaller value returns with a value of infinity, which it should test just like the other fucntion.
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):
    for key in data_dict.keys():
        largest = float('-inf')
        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):
    for key in data_dict.keys():
        smallest = float('inf')
        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()

and the output is:

>>> 
********************
Linear Interpolation data
********************

********************
Largest rms:
4.0477e-07 rxn: 14948
********************
Smallest rms:
inf rxn: 14948
********************
Max error:
0.00016591 rxn: 14948
********************
Largest T_max:
32063000.0 rxn: 14948
********************
Largest R_max:
129470.0 rxn: 14948
********************
Largest Local max:
129450.0 rxn: 14948
********************

********************
Logarithmic Interpolation data
********************

********************
Largest rms:
3.2459e-07 rxn: 14948
********************
Smallest rms:
inf rxn: 14948
********************
Max error:
0.00014016 rxn: 14948
********************
Largest T_max:
32063000.0 rxn: 14948
********************
Largest R_max:
129460.0 rxn: 14948
********************
Largest Local max:
129450.0 rxn: 14948
********************

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2