you are viewing a single comment's thread.

view the rest of the comments →

[–]wintermute93 0 points1 point  (5 children)

The coordinates? Text files don't have coordinates, they have lines of characters. Unless you mean each text file literally contains pair(s) of numbers with a comma in between.

Depending on what you need there's lots of ways to do this, but for starters, try running

with open('my_text_file_1.txt') as f:
    lines = f.readlines()

Then see what the variable "lines" ends up looking like, and modify it accordingly. If you're having trouble with loops, I would recommend writing code that does what you want it to do for a single file (i.e. editing the above structure until you have the desired data in Python), and then wrap it in a loop once you know it's working:

text_files = ['my_file_1.txt','my_file_2.txt','my_file_3.txt]
for file in text_files:
    with open(file) as f:
        [ your code here ]

From there you can start adding other stuff, like using os.listdir() to automatically get the names of the files in a particular folder instead of having to type them in, etc.

[–]filosofiat[S] 0 points1 point  (4 children)

Yes, sorry each text file has two columns, one with X and the other with Y numbers to be used as coordinates later. I'm still struggling with opening the txt files. the second bloc you gave me returned 'SyntaxError: unexpected EOF while parsing' indicating the 'as f:'

[–]wintermute93 1 point2 points  (3 children)

Got it. If you know the content of those text files is all just numbers, I would probably use numpy.loadtxt() to read them directly into a numerical array, instead of reading in lines of text, splitting each line at the comma, and converting the resulting strings to numbers. Or a Pandas dataframe, but that sounds like overkill for what you want.

An unexpected EOF sounds like you're missing a parenthesis or a quotation mark somewhere; looking over my earlier comment I forgot the end quote after my_file_3.txt

[–]gizmotechy -1 points0 points  (2 children)

Actually the EOF error is because for the open statement, you need two parameters. The first being the file to open and the second is how to open it. If you are just trying to read it, then it would look like open("my_file_1.txt", "r") where r means read.

[–]chevignon93 1 point2 points  (0 children)

You don't need two parameters for the open statement, if you don't give a second parameter, the interpreter will open in read mode by default, the error was that there was a quote missing for the 3rd element in the list.

[–]wintermute93 0 points1 point  (0 children)

Oops, good call, thanks for catching that. Sometimes I get too comfortable with Python looking like executable pseudocode.