我只是在学习 Python 以在 Inkscape 中进行扩展,并且在比较从文件中加载的字符串时遇到了问题。我要做的是加载我在文本文件中定义的多边形:
polygon
r:255
g:0
b:0
50;50
50;100
100;50
我的解析方法是这样的:
def load_file(filepath, parent, log):
file = open(filepath)
x = []
y = []
r = 0
g = 0
b = 0
index = 0
for line in file:
fline = line.lstrip("\xef\xbb\xbf").rstrip("\n")
log.write("Input string: " + repr(line) + "\n")
log.write("Formatted: " + repr(fline) + "\n")
if fline == "":
continue
elif fline is "polygon": ## Where the first line should be going
log.write("\tDetected string as polygon start delimiter\n")
if index > 0:
draw_shape(x, y, r, g, b, "Polygon", parent)
del x[0, len(x)]
del y[0, len(y)]
r = g = b = index = 0
continue
elif fline[:2] is "r:":
log.write("\tDetected string as polygon red value delimiter\n")
r = int(fline[2:])
continue
elif fline[:2] is "g:":
log.write("\tDetected string as polygon green value delimiter\n")
g = int(fline[2:])
continue
elif fline[:2] is "b:":
log.write("\tDetected string as polygon blue value delimiter\n")
b = int(fline[2:])
continue
else: ## Where the first line actually is going
log.write("\tDelimiter failed previous detections; assumed to be polygon cordinates\n")
spl = fline.split(";")
x[index] = float(spl[0]) ## Error gets thrown here
y[index] = float(spl[1])
index += 1
continue
draw_shape(x, y, r, g, b, parent)
这种方法在第一行就失败了。它不断看到“多边形”并进入最后一个 else 块,在那里它解析坐标。我一直保留的日志文件如下所示:
Process Started
Input string: '\xef\xbb\xbfpolygon\n'
Formatted: 'polygon'
Delimiter failed previous detections; assumed to be polygon coordinates
我已经在 shell 中逐步完成了这个过程,在那里它说line is "process"
是真的,所以我完全迷失了这里。有什么帮助吗?