2017-09-26 130 views
-2

Stackoverflow每次以某种方式切断这部分消息..无论如何:我试图在python中做出一种特殊的搜索功能。基本上它忽略了较高/较低的情况和空间。 “and”被认为是相同的,所有的都很好,我可以打开一个文本文件并在其中找到“path”这个词,但是当我试图寻找“d = \”“时,我得到一个非常奇怪的错误。显然,我正在用一个NoneType变量索引一个字符串......但是,上面的7行代码以完全相同的方式编制索引并在那里工作!TypeError:字符串索引必须是整数,而不是无类型

这里是我的代码

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

如果“specialFind”到达其结尾,则隐式返回“无”。这个'None'可以通过'sIndex'反馈给它。 –

+0

就是这样!非常感谢您打开我的眼睛。经过一段时间的调试,我只是盲目。 – Anteino

回答

0

正如Michael已经指出的那样,specialFind不会返回一个int值,python也不会警告您发送的变量的类型。其中一个时代python的宽容性格不太实用吧。

相关问题