简体   繁体   English

使用Python从CSV绘制像素坐标

[英]Plotting pixel coordinates from CSV using Python

From the image i have extracted the pixel co-ordinates ( x,y). 我从图像中提取了像素坐标(x,y)。 To validate the co-ordinates , i am trying to plot those pixel co-ordinates. 为了验证坐标,我试图绘制那些像素坐标。 But i couldn't do it. 但是我做不到。

I tried to plot using turtle but still i am unable to do it 我试图用乌龟密谋,但我还是做不到

import turtle
import math

def drawMap():
    filename = r"build_coords.csv"

    trace = turtle.Turtle()
    trace.up()

    #scr = Screen()

    with open(filename, 'r') as f_input:
        for row in f_input:
            row = row.strip('()\n').split(',')
            x = float(row[0])
            y = float(row[1])
            trace.goto(x,y)
            trace.write(".")
    raw_input()
    #scr.mainloop()
drawMap()

ValueError: could not convert string to float: '0\\t3' ValueError:无法将字符串转换为浮点型:'0 \\ t3'

Please kindly help to fix this. 请帮助解决此问题。 Thanks in advance 提前致谢

Your input logic: 您的输入逻辑:

x, y = row.strip('()\n').split(',')

seems to imply input of the form: 似乎暗示形式的输入:

(10, 20)
(30, 40)

which is not CSV. 不是CSV。 Your error message seems to imply input of the form: 您的错误消息似乎暗示输入了以下形式:

10\t20
30\t40

So the key to answering your question correctly is for you to show us some sample input. 因此,正确回答问题的关键是让您向我们展示一些示例输入。 Below is a rework of your code: 下面是对代码的重做:

from turtle import Turtle, Screen

FILENAME = "build_coords.csv"

def drawMap(filename):
    trace = Turtle(visible=False)
    trace.penup()

    with open(filename) as f_input:
        header = f_input.readline().rstrip()  # "X,Y"

        for row in f_input:
            x, y = row.rstrip().split(',')  # 10,20\n
            trace.goto(float(x), float(y))
            trace.dot(2)

screen = Screen()

drawMap(FILENAME)

screen.exitonclick()

UPDATE 更新

Based on your comments, I'm now assuming the data is CSV and looks like: 根据您的评论,我现在假设数据为CSV,如下所示:

X,Y
0.0,3.0
0.0,4.0
0.0,5.0
0.0,6.0
0.0,8.0
0.0,10.0
0.0,11.0
0.0,15.0
0.0,16.0

I've updated the above code accordingly. 我已经相应地更新了上面的代码。

Error message says that there is a tabulation ('\\t'-character) inside your text, which is not removed in yor strip command. 错误消息指出您的文本内有一个制表符('\\ t'-字符),您的strip命令中并未将其删除。 The '\\t' character is still there when you try to convert a string to the floating point which causes the ValueError. 当您尝试将字符串转换为导致ValueError的浮点时,“ \\ t”字符仍然存在。

So, you could try find out why the input file has tabulations in the first place or strip those away too along with other whitespace characters. 因此,您可以尝试找出输入文件为何首先包含表格的原因,或者将其与其他空格字符一起删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM