Home > Software engineering >  How to read plot points from a text file, reading each line of text individually?
How to read plot points from a text file, reading each line of text individually?

Time:12-11

So I am very new to coding, and I have to draw an image in python using multiple separate polygons with turtle with the plot points all contained in a single .txt file. I have my file set up, so each set of points is on a different line of text, but I'm just not sure how to call each line of text individually to the program. Like this:

-30,9,108,5,110,16,33,72,-42,75,-30,9
-171,15,-56,10,-64,78,-161,77,-171,15
-201,17,-182,75,-322,75,-340,18,-201,17
-378,-32,-366,-31,-361,17,-335,84,-345,84,-372,18,-378,-32
366,-24,355,-27,355,-45,372,-45,366,-24
-149,-2,-187,0,-187,-7,-150,-6,-149,-2
-1,-8,-37,-4,-37,-10,-2,-11,-1,-8

Here is the code that I have so far, not including the code that involves actually drawing the image:

import turtle
import os
import re

file_directory = os.path.dirname(__file__)

movements = ""

with open(file_directory   '\\plotpoints.txt', "r") as plotme:
    movements = movements   plotme.readlines()

plotme.close()

pointlist = movements.split(",")

for counter in range(0, len(pointlist)):
    pointlist[counter] = int(pointlist[counter])

Like I said, I'm very new to coding, so anything at all to help me understand this better would be greatly appreciated.

CodePudding user response:

Your attempt was good, but to achieve the result you need to do this:

  1. remove plotme.close() as this is handled by the with block (context manager).
  2. use os.path.join() if you are joining paths.
  3. for loop inside the with block to read all lines to a list.
import os

file_directory = os.path.dirname(__file__)
full_path = os.path.join(file_directory,'plotpoints.txt')

movements = []

with open(full_path, "r") as plotme:
    for line in plotme.readlines():
        movements.extend(line.strip().split(","))
    
# convert strings to ints
movements = [int(x) for x in movements]

print(movements)

result:

[-30, 9, 108, 5, 110, 16, 33, 72, -42, 75, -30, 9, -171, 15, -56, 10, -64, 78, -161, 77, -171, 15, -201, 17, -182, 75, -322, 75, -340, 18, -201, 17, -378, -32, -366, -31, -361, 17, -335, 84, -345, 84, -372, 18, -378, -32, 366, -24, 355, -27, 355, -45, 372, -45, 366, -24, -149, -2, -187, 0, -187, -7, -150, -6, -149, -2, -1, -8, -37, -4, -37, -10, -2, -11, -1, -8]

you can then use this result for plotting...

Here is an alternative option for producing a list of lists for each shape:

import os

file_directory = os.path.dirname(__file__)
full_path = os.path.join(file_directory,'plotpoints.txt')

movements = []

with open(full_path, "r") as plotme:
    for line in plotme.readlines():
        shape = line.strip().split(",")
        shape = [int(x) for x in shape]
        movements.append(shape)

print(movements)

An the result looks like this:

[[-30, 9, 108, 5, 110, 16, 33, 72, -42, 75, -30, 9],
 [-171, 15, -56, 10, -64, 78, -161, 77, -171, 15],
 [-201, 17, -182, 75, -322, 75, -340, 18, -201, 17],
 [-378, -32, -366, -31, -361, 17, -335, 84, -345, 84, -372, 18, -378, -32],
 [366, -24, 355, -27, 355, -45, 372, -45, 366, -24],
 [-149, -2, -187, 0, -187, -7, -150, -6, -149, -2],
 [-1, -8, -37, -4, -37, -10, -2, -11, -1, -8]]
  • Related