0

From the image i have extracted the pixel co-ordinates ( 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'

Please kindly help to fix this. Thanks in advance

2 Answers 2

1

Your input logic:

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

seems to imply input of the form:

(10, 20)
(30, 40)

which is not 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:

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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
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 ### input above ###
ValueError: need more than 1 value to unpack , if i try to use the input above am getting error like this
@vishal, I've updated my code to match the data in your comments -- I don't understand why your original code strips parentheses from the start and end of the data since there aren't any.
How to plot it faster ? Can i give Any other colour ? Please help me through it.
0

Error message says that there is a tabulation ('\t'-character) inside your text, which is not removed in yor strip command. The '\t' character is still there when you try to convert a string to the floating point which causes the ValueError.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.