2012-10-25 27 views
0

所以几天前,SOCommunity帮助我解析器。即这样的:递归中的非类型的解析器错误

def MyList(self): 
    class _MyList(list): 
     def __init__(self, parent): 
      self.parent = parent 
      list.__init__(self) 
    top = current = _MyList(None) 
    ntab_old = 0 
    for line in self.raw_data: 
     ntab = line.count('\t') 
     if(ntab_old > ntab): 
      for _ in range(ntab_old-ntab): 
       current = current.parent 
     elif(ntab_old<ntab): 
      current.append(_MyList(current)) 
      current = current[-1] 
     current.append(line.strip('\t').strip('|')) 
     ntab_old =ntab 
    return top 

解析通过数据是这样的:

|[nothing detected] www.neopets.com/ 
     |status: (referer=http://www.google.com)saved 55189 bytes /fetch_d4cd213a56276ca726ddd437a1e75f1024ab7799 
     |file: fetch_d4cd213a56276ca726ddd437a1e75f1024ab7799: 55189 bytes 
     |file: decoding_367af53b4963986ecdeb9c09ce1a405b5b1ecd91: 68 bytes 
     |[nothing detected] (script) images.neopets.com/js/common.js?v=6 
      |status: (referer=http://www.google.com)saved 1523 bytes /fetch_8eeadcc08d4cb48c02dedf18648510b75190d1d7failure: [Errno 13] Permission denied: '/tmp/tmpsha1_8d7fb3ff1ef087c7ea2bf044dee294735a76ed4b.js' 
      |file: fetch_8eeadcc08d4cb48c02dedf18648510b75190d1d7: 1523 bytes 

这应该返回包含每个子部分的嵌套列表。然而,从哪儿冒出来,我得到这些错误:

 current.append(line.strip('\t').strip('|')) 
AttributeError: 'NoneType' object has no attribute 'append' 

任何帮助,将不胜感激

+0

您上面张贴的数据具有创建缩进的制表符和空格的不规则混合。你的数据真的是这样吗? – unutbu

+0

你可以发布数据片段的'repr'吗?这会向我们显示标签所在的位置。 – unutbu

回答

2
current.append(...) 
AttributeError: 'NoneType' object has no attribute 'append' 

是说,在某些时候currentNone

那么如何将current设置为None?我的猜测是,它可能发生在这里:

 for _ in range(ntab_old-ntab): 
      current = current.parent 

如果电流设置为其父往往不够,因为在顶层,_MyListNone作为父实例化它最终将被设置为None

top = current = _MyList(None) 

这段代码也可能会出现问题:

elif(ntab_old<ntab): 
     current.append(_MyList(current)) 
     current = current[-1] 

如果ntab是3,而ntab_old是1,那么可能需要移动两级“缩进”,但代码只创建一个嵌套的_MyList

您可能会认为您的注意力水平不会跳过两次。但是这可能很难用你的眼睛来分辨,特别是如果数据很长。数据可能不像我们想象的那样规则。

另外,如果在|的右侧存在'\t'的偏离,那么它会影响ntab计数,即使它可能不应该影响缩进级别。

+0

哦,我明白你的意思了。但是,如何防止这种情况发生? :S我想你成为父母的唯一方法是如果你有'0'标签 –