2017-09-26 9 views
-2

何とかいつもメッセージのこの部分を切り詰めている。とにかく:私はPythonで特別な検索機能を作ろうとしている。基本的には、大文字/小文字とスペースを無視するものです。また、 'と'は等しいとみなされます。すべて正常に動作し、テキストファイルを開いて「パス」という単語を見つけることができます。しかし、「d = \」を探すときには、 。どうやら、私はNoneType変数で文字列のインデックスを作成しています...しかし、上の7行は、私はまったく同じ方法でインデックスを作成しており、その上で動作します!ここでTypeError:文字列のインデックスは整数でなくてはならないNoneType

私のコード

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() 

であり、これは私がオープニングだファイルです。それは私がSVGファイルを分離するために保存するために切断ポリゴンに分割しようとしているSVGだ

<?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> 

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 

私は本当にそれを取得しないでください:

これは、Pythonスクリプトが私を与えている出力されます。何か案は?

+0

'specialFind'は、最後に到達すると暗黙的に' None'を返します。この 'None'は'Index'でそれにフィードバックされるかもしれません。 –

+0

それだけです!私の目を開けてくれてありがとう。デバッグのしばらくして、私は盲目になってしまいました。 – Anteino

答えて

0

マイケルはすでにspecialFindがintを返さないことを指摘しているので、Pythonは返信する変数のタイプについて警告しません。これらの時代の一つPythonの寛容な性格はあまり実用的ではありません。

関連する問題