Stackoverflow每次都会以某种方式切断这部分消息..无论如何:我试图在python中创建一种特殊的搜索功能。基本上它是忽略高/小写和空格的一个。还'和"被认为是平等的。一切运作良好,我可以打开一个文本文件,找到" path"在里面。但是当我试图寻找" d = \""我得到一个非常奇怪的错误。显然我用一个NoneType变量索引一个字符串......但是我以上的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
我真的不明白。有什么想法吗?
答案 0 :(得分:0)
正如迈克尔已经指出的那样,specialFind不会返回一个int,并且python不会警告你发送回的变量类型。其中一次python的宽容角色不太实用哦。