Python 3.2.1:从文本文件中错误读取

发布于 2024-12-11 02:10:05 字数 1511 浏览 0 评论 0原文

我有一个文本文件,其中包含圆心的 x 坐标和 y 坐标,后跟半径以及圆的填充颜色。

文本文件(供参考):

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

我发现的整个程序运行良好,除了一部分。我使用 for 循环从文件中获取信息,根据需要将其分割,并将圆圈绘制到 GraphWin。

问题是,文本文件中有 15 个圆的数据,但 for 循环只读取 14 个圆,完全跳过第一行。

代码:

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()

如何让它不跳过文本文件中的第一行?我在互联网和这里搜索过这个问题,但通常我得到的都是人们询问如何跳线,这与我需要的相反。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期情话 2024-12-18 02:10:05

我认为你只需要在 for 循环之前剪掉这一行:

lines = inFile.readline()

那就是在逐行浏览文件之前读取第一行,你不执行任何操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文