I am relatively new to python, and have run into an issue with reading a strangely formatted output file.
The current format of the output file for each line is [(x,y),.....(x,y)] (tuples nested within a list?) and I need to read each line in the file and output a 3 columned file with columns X Y and Flavor where flavor is the i-th component in a defined variable.


Could you post an extract of the input file and an example of what you would like in the output one...

One input (the one with the co-ordinates) each of the lines (5500 in this case, must be able to do for different lengths) is in the format that follows

there is also in the program already defined a variable Prot=HPPHPPHHP

the desired outcome is that the program read through each line in the file and for each line output a different file with the format:
0 1 H
1 1 P
2 1 P
2 2 H
1 2 P
0 3 P
-1 4 H
-2 4 H
-1 5 P

The other important thing is that the program is able to account for the fact that it will be run with files of different length in 3 different ways.
1) Number of sets of co-ordinates
2) Number of co-ordinates in a set
3) Length of Prot (= (2) number of co-ordinates in a set)

you've got an easy way of doing this :

for data in datas:
    exec ("lst=%s" % data)
    for i, coord in enumerate(lst):
        for elt in coord:
            print elt,
        print Prot[i]

This is to be adapted for your exact need

i see how this could work, but how can i get Data to be from a read in of an existing file. i tried using openfile and then readlines but then when i try to split the data after it has been readlines'ed i get an error 'list' object has no attribute 'split' which i guess means that my file is read in like a list so then how do i make it read like a string

hmmm i used data=open('file','r') and then Data=data.read() and it actually read it as i would expect it to read it.... hmmm

You can also do like this.
This avoids to load the whole file in memory...

for line in file("myfile.txt"):
    exec ("lst=%s" % line.strip())
    for i, coord in enumerate(lst):
        for elt in coord:
            print elt,
        print Prot[i]
This article has been dead for over six months. Start a new discussion instead.