Search code examples
pythonpython-3.xfor-loopreadlines

Multiple commands within a for-loop, multiple for-loops, or something else?


For a task (i.e. no pandas or anything that would make it easier), I have to plot a polygon in python3/numpy/Matplotlib, but the data file is giving me trouble at the first hurdle and I can't figure out what is causing problems.

The .dat file has 2 columns of several lines, each line looking like this:

(34235.453645, 68597.5469)

So far I have figured out how to remove the brackets and the comma in every line with .replace so it makes:

34235.453645 68597.5469

with this:

lang-js
#Assign columns
data_easting=[]
data_northing=[]
    
#Open the .dat file (in Python)
Poly = open('poly.dat','r')
    
#Loop over each line of poly.dat.
for replace in Poly.readlines():
    #Replace the extraneous information ("(",",",")") with nothing, rather than multiple commands to strip and remove them. Stack Overflow Source for idea: https://stackoverflow.com/questions/390054/python-strip-multiple-characters.
    replace = replace.replace('(','').replace(',','').replace(')','')

#Loop over the replaced data to split into lines 1(easting) and 2(northing) and append.

However when I want to split the returned "replace" string into x and y (the data_northing and _easting lists) after the final comment with another for-loop within:

        for line in replace.readlines():
                x,y=line.split()
                data_easting.append(x)
                data_northing.append(y)

I get an error saying that the str "replace" can't use the readlines function.

If I remove the indentation (both for-loops at the same level), the same error appears.

If I replace "replace" with "Poly" from the previous for-loop, the "list" is only the first line of x,y data (print (x[n]) just returns the nth character of the number string)

If I combine the loops and commands together like so;

for replace, line in Poly.readlines():
        x,y =line.split
        data_easting.append(x)
        data_northing.append(y)

and try the command I get an error that "Line is not defined", or I get a value error: "ValueError: not enough values to unpack (expected 2, got 1)"

As it's probably evident by now, I'm very new to Python and can't figure out how to get past this to move on to plotting data (which I'm mostly ok with). How would I make the second function follow from the first, do I have make the first function make an actual output and then run second command (i.e. "Make Poly2.dat" and then split that datafile)?

When I've searched around for the problem, a lot of solutions bring up iterators. Could that be applied here?

EDIT: I ended up scrapping this and going for .strip('()\n' and .split(', ') to get the data, however now while I have 2 lists of strings with only the numbers I need, I still get a Value Error; not enough values to unpack (expected 2, got 1).


Solution

  • You seem to be pushing your code in all the wrong directions. You need to learn how to debug small programs.

    With data processing like this you should print out the results as you go and you will see what the data looks like at each stage.

    Perhaps you meant this:

    data_easting=[]
    data_northing=[]
        
    #Open the .dat file (in Python)
    Poly = open('poly.dat','r')
        
    #Loop over each line of poly.dat.
    for replace in Poly.readlines():
        replace = replace.strip()
        if replace:
            replace = replace.replace('(','').replace(',','').replace(')','')
            print(replace)
            x,y = replace.split()
            data_easting.append(x)
            data_northing.append(y)
    

    Update: Used strip() to clean up blank lines.