I am trying to write a Python code to extract the coordinates from the "inkscape" gcode file and use these coordinates as input at another function. Bay the way I am very beginner at programming and also Python. I wrote a very simple few lines, u can see below. When I try to run the code I am getting "AttributeError: 'NoneType' object has no attribute 'start' " error. I think it is about at the begging of the loop "xindex.start()" returns with null, so guess I need to define an initial value, but I couldn't find how to do it. The example code is only for the X value.
import re
with open("example.gcode", "r") as file:
for line in file:
if line.startswith('G00') or line.startswith('G01'): # find the lines which start with G00 and G01
xindex = re.search("[X]", line) # search for X term in the lines
xindex_number = int(xindex.start()) # find starting index number and convert it to int
The inside of the gcode looks like :
S1; endstops
G00 E0; no extrusion
G01 S1; endstops
G01 E0; no extrusion
G21; millimeters
G90; absolute
G28 X; home
G28 Y; home
G28 Z; home
G00 F300.0 Z20.000; pen park !!Zpark
G00 F2400.0 Y0.000; !!Ybottom
....
Any help is appreciated
Wish u all have a nice day
CodePudding user response:
AttributeError: 'NoneType' object has no attribute 'start'
means that you are trying to call .start()
on an object that is equal to None.
You code is looking for the first line that starts with 'G00' or 'G01' which in this case would be the line: "G00 E0; no extrusion" and then it is trying to find where the letter X exists in that line.
In this case, 'X' does not exist in that line, hence xindex = None
. Therefore, you cannot call xindex.start()
without throwing an error. This is what the error is telling you.
CodePudding user response:
Add an if
condition and it should work fine
import re
with open("example.gcode", "r") as file:
for line in file:
if line.startswith("G00") or line.startswith("G01"):
xindex = re.search("[X]", line)
# Check if a match was found
if xindex:
xindex_number = int(xindex.start())
And refer to @QuantumMecha 's answer to understand why.