Stackoverflow每次都会剪切邮件的这部分。。无论如何:我正在尝试在python中创建一种特殊的搜索函数。基本上它忽略了大小写和空格。“和”也被认为是相等的。所有的工作都很好,我能够打开一个文本文件,找到“路径”一词。但当我试图寻找“d=\”时,我得到了一个非常奇怪的错误。显然我在用一个非类型变量索引一个字符串。。。但是上面的7行我用完全相同的方式索引它,在那里它工作!!你知道吗
这是我的密码
import wx
class Mywin(wx.Frame):
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title = title,size = (400, 200))
self.panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
self.bButton = wx.Button(self.panel, -1, "Vector bestand kiezen", pos=(10, 10))
self.bButton.Bind(wx.EVT_BUTTON, self.bButton_clicked)
self.sb = self.CreateStatusBar()
self.panel.SetSizer(vbox)
self.panel.Layout()
self.Centre()
self.Show()
self.Fit()
def bButton_clicked(self, event):
with wx.FileDialog(self, "Open vector file", wildcard="Scalable vector graphic (*.svg)|*.svg|Drawing exchange format (*.dxf)|*.dxf", style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:
if fileDialog.ShowModal() == wx.ID_CANCEL:
return # the user changed their mind
# Proceed loading the file chosen by the user
self.pathname = fileDialog.GetPath()
self.sb.SetStatusText("File was opened. Finding separated paths.")
self.extractPaths()
return
def specialFind(self, sIndex, substring):
eIndex = sIndex
findingStart = 1
i = int(0)
while(i < len(substring)):
print("i: {}".format(i))
if(i == 0):
print("looking for the start")
findingStart = 1
else:
findingStart = 0
if(substring[i] == "\'"):
while(1):
if((self.text[eIndex] == "\'") | (self.text[eIndex] == "\"")):
print(self.text[eIndex])
eIndex = eIndex + 1
break
elif((self.text[eIndex] == " ") | (findingStart == 1)):
eIndex = eIndex + 1
if(eIndex == len(self.text)):
return -1
continue
else:
eIndex = eIndex + 1
i = -1
break
elif((ord(substring[i]) >= 65) & (ord(substring[i]) <= 122)):
print("Looking for the letter: {}".format(substring[i]))
if(ord(substring[i]) <= 90):
sign = 1
else:
sign = -1
while(1):
print("{}, type: {}".format(eIndex, type(eIndex)))
if( ord(self.text[eIndex]) == ord(substring[i]) ):
# | (ord(self.text[eIndex]) == ord(substring[i]) + sign * 32)):
print("{}:: {}".format(self.text[eIndex], eIndex))
eIndex = eIndex + 1
break
elif((ord(self.text[eIndex]) == ord(" ")) | (findingStart == 1)):
print("{} != {}".format(ord(self.text[eIndex]), ord(substring[i])))
eIndex = eIndex + 1
if(eIndex == len(self.text)):
print("searched whole document")
return -1
continue
else:
print("{} != {}".format(self.text[eIndex], substring[i]))
print("trying again")
eIndex = eIndex + 1
i = -1
break
else:
while(1):
if(ord(self.text[eIndex]) == ord(substring[i])):
print("{}:: {}".format(self.text[eIndex], eIndex))
eIndex = eIndex + 1
break
elif((ord(self.text[eIndex]) == ord(" ")) | (findingStart == 1)):
eIndex = eIndex + 1
if(eIndex == len(self.text)):
return -1
continue
else:
eIndex = eIndex + 1
i = -1
break
i = i + 1
def extractPaths(self):
self.pathAmount = 0
file = open(self.pathname, "r")
self.text = file.read()
self.textLength = len(self.text)
pathIndex = 0
dsIndex = 0
deIndex = 0
while(1):
pathIndex = self.specialFind(deIndex, "<path")
if(pathIndex == -1):
print("No path found this time.")
break
dsIndex = self.specialFind(pathIndex, "d=\"")
deIndex = self.specialFind(dsIndex, "\"")
if((dsIndex == -1) | (deIndex == -1)):
self.sb.SetStatusText("File is corrupted. Path tag was opened, but never properly closed.")
break
self.pathAmount = self.pathAmount + 1
print("pathAmount: {}".format(self.pathAmount))
return
print("<: {}".format(ord('<')))
# print("a: {}, A: {}".format(ord('a'), ord('A')))
# for i in range(ord('A'), ord('z') + 6):
# print("{}".format(chr(i)))
app = wx.App()
Mywin(None, 'Vector splitter.')
app.MainLoop()
这是我要打开的文件:
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="4" height="4" viewBox="-2 -2 4 4" xmlns="http://www.w3.org/2000/svg" version="1.1">
<title>OpenSCAD Model</title>
<path d="
M 2,-0 L -1,-1.73205 L -1,1.73205 z
" stroke="black" fill="lightgray" stroke-width="0.5"/></svg>
这是一个SVG,我正试图将其拆分为断开连接的多边形,以保存到单独的SVG文件中。你知道吗
这是python脚本给我的输出:
C:\Users\Anteino\Desktop\python svg splitter>python gui.py
<: 60
i: 0
looking for the start
<:: 0
i: 1
Looking for the letter: p
1, type: <type 'int'>
t != p
trying again
i: 0
looking for the start
<:: 21
i: 1
Looking for the letter: p
22, type: <type 'int'>
/ != p
trying again
i: 0
looking for the start
<:: 30
i: 1
Looking for the letter: p
31, type: <type 'int'>
p:: 31
i: 2
Looking for the letter: a
32, type: <type 'int'>
a:: 32
i: 3
Looking for the letter: t
33, type: <type 'int'>
t:: 33
i: 4
Looking for the letter: h
34, type: <type 'int'>
h:: 34
i: 0
looking for the start
Looking for the letter: d
None, type: <type 'NoneType'>
Traceback (most recent call last):
File "gui.py", line 31, in bButton_clicked
self.extractPaths()
File "gui.py", line 119, in extractPaths
dsIndex = self.specialFind(pathIndex, "d=\"")
File "gui.py", line 68, in specialFind
if( ord(self.text[eIndex]) == ord(substring[i]) ):
TypeError: string indices must be integers, not NoneType
我真的不明白。有什么想法吗?你知道吗
正如Michael已经指出的,specialFind不会返回int,python也不会警告您要返回的变量类型。有一次,python的宽容性格并不那么实际。你知道吗
相关问题 更多 >
编程相关推荐