html92
html92

Reputation: 65

Python 3.2.1: Incorrectly reading from text file

I have a text file that contains the x- and y-coordinates for the center of a circle, followed by the radius, and the fill color for the circle.

THE TEXT FILE (for reference):

30,50 12 goldenrod
78,75 10 coral
14,79 11 tomato
32,77 12 maroon
21,25 15 burlywood
24,67 14 sienna
62,93 13 chartreuse
24,42 16 olivedrab
79,18 10 peachpuff
18,61 19 thistle
15,27 11 mediumpurple
84,87 12 cornsilk
77,25 11 linen
74,96 15 honeydew
63,15 13 dodgerblue

The entire program that I figured out works fine, except for one part. I use a for loop to take in the information from the file, split it as necessary, and draw the circles to the GraphWin.

The problem is, there is data for 15 circles in the text file, but the for loop is reading only 14 circles, completely skipping over the first line.

THE CODE:

def myCircles():
    path = "C:\\"
    extension = ".txt"
    center = ""
    radius = ""
    color = ""
    lines = ""

    fname = input("Please enter the name of the file where the data is stored. The path [C:\\...] and extension [*.txt] are not needed: ") or "TestDataCircles"

    inFile = open(path + fname + extension, "r")

    win = GraphWin("WSUTC CptS111-Q2", 500, 500)
    win.setCoords(0, 0, 100, 100)

    lines = inFile.readline()

    for lines in inFile.readlines():
        center, radius, color = lines.split()
        centerx, centery = center.split(",")
        centerx, centery = eval(centerx), eval(centery)

        ccenter = Point(centerx, centery)

        myCircle = Circle(ccenter, eval(radius))
        myCircle.setFill(color)
        myCircle.draw(win)

    inFile.close()

    print("Please click anywhere inside the graphics window to close it.")
    win.getMouse()
    win.close()

How do I get it to NOT skip the first line in the text file? I have searched for this problem on the Internet and here, but generally what I am getting is people asking how to skip lines, which is the opposite of what I need.

Upvotes: 2

Views: 992

Answers (1)

aganders3
aganders3

Reputation: 5945

I think you just need to cut out this line before your for loop:

lines = inFile.readline()

That's reading the first line, which you do nothing with, before you go through the file line-by-line.

Upvotes: 6

Related Questions