2017-09-26 80 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的寬容性格不太實用吧。

相關問題