2013-02-21 115 views
1

此代码几乎完全是从scipy.org cookbook recipe键入,我还没有注意到任何错字,所以也许代码被oudated?为什么此代码正确解析数字,但在KeyWord()和QuotedString()方法上失败?ConfigNumParser ValueError:无效文字为int()与基数10:''

#use the Regex element to rapidly detect strings representing numbers: 
from re import VERBOSE 
number = Regex(r""" 
       [+-]? #optional sign 
       (
        (?:\d+(?P<float1>\.\d*)?) # match 2 or 2.02 
        |        # or 
        (?P<float2>\.\d+)?   # match .02 
       ) 
       (?P<float3>[Ee][+-]?\d+)?   #optional exponent 
       """, flags=VERBOSE 
       ) 
# a function to convert this string into python float or integer and set a 
# parseAction to tell pyparsing to automatically convert a number when it finds 
# one: 
def convert_number(t): 
     """Convert a string matching a number to a python number""" 
     print "Converting " + str(t) 

     if t.float1 or t.float2 or t.float3: 
      return [float(t[0])] 
     else: 
      return [int(t[0])] 
      #try: 
      # return [int(t[0])] 
      #except: 
      # return t 

number.setParseAction(convert_number) 

# create a list of element converting strings to python objects: 
from numpy import NAN 
pyvalue_list = [ 
       number, 
       Keyword('True').setParseAction(replaceWith(True)), 
       Keyword('False').setParseAction(replaceWith(False)), 
       Keyword('NAN', caseless=True).setParseAction(replaceWith(NAN)), 
       Keyword('None').setParseAction(replaceWith(None)), 
       QuotedString('"""', multiline=True), 
       QuotedString("'''", multiline=True), 
       QuotedString('"'), 
       QuotedString("'"), 
       ] 

pyvalue = MatchFirst(e.setWhitespaceChars(' \t\r') for e in pyvalue_list) 

按照食谱我的输出应该是:

>>> test2 = ''' 
>>>  1 2 3.0 0.3 .3 2e2 -.2e+2 +2.2256E-2 
>>>  True False nan NAN None 
>>>  "word" "two words" 
>>>  """'more words', he said""" 
>>> ''' 
>>> print pyValue.searchString(test2) 
[[1], [2], [3.0], [0.29999999999999999], [0.29999999999999999], [200.0], [-20.0], [0.022256000000000001], 
[True], [False], [nan], [nan], [None], ['word'], ['two words'], ["'more words', he said"]] 

,但我得到ValueError异常:对于int无效的文字()基数为10:“”,所以我增加了一个print语句来帮助调试,这里是终端会话:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import ConfigNumParser as parser 
>>> test2 = ''' 
...  1 2 3.0 0.3 .3 2e3 -.2e+2 +2.2256E-2 
...  True False nan NAN None 
...  "word" "two words" 
...  """'more words', he daid""" 
... ''' 
>>> print parser.pyvalue.searchString(test2) 
Converting ['1'] 
Converting ['2'] 
Converting ['3.0'] 
Converting ['0.3'] 
Converting ['.3'] 
Converting ['2e3'] 
Converting ['-.2e+2'] 
Converting ['+2.2256E-2'] 
Converting [''] 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "C:\Python27\Lib\site-packages\pyparsing.py", line 1099, in searchString 
    return ParseResults([ t for t,s,e in self.scanString(instring, maxMatches) ]) 
    File "C:\Python27\Lib\site-packages\pyparsing.py", line 1036, in scanString 
    nextLoc,tokens = parseFn(instring, preloc, callPreParse=False) 
    File "C:\Python27\Lib\site-packages\pyparsing.py", line 871, in _parseNoCache 
    loc,tokens = self.parseImpl(instring, preloc, doActions) 
    File "C:\Python27\Lib\site-packages\pyparsing.py", line 2451, in parseImpl 
    ret = e._parse(instring, loc, doActions) 
    File "C:\Python27\Lib\site-packages\pyparsing.py", line 897, in _parseNoCache 
    tokens = fn(instring, tokensStart, retTokens) 
    File "C:\Python27\Lib\site-packages\pyparsing.py", line 660, in wrapper 
    ret = func(*args[limit[0]:]) 
    File "ConfigNumParser.py", line 33, in convert_number 
    return [int(t[0])] 
ValueError: invalid literal for int() with base 10: '' 

所以在搜索了几个建议后,我在上面的注释区域添加了try-catch。现在的结果:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import ConfigNumParser as parser 
>>> test2 = ''' 
...  1 2 3.0 0.3 .3 2e3 -.2e+2 +2.2256E-2 
...  True False nan NAN None 
...  "word" "two words" 
...  """'more words', he daid""" 
... ''' 
>>> print parser.pyvalue.searchString(test2) 
Converting ['1'] 
Converting ['2'] 
Converting ['3.0'] 
Converting ['0.3'] 
Converting ['.3'] 
Converting ['2e3'] 
Converting ['-.2e+2'] 
Converting ['+2.2256E-2'] 
Converting [''] 
Converting [''] 
Converting [''] 
<deleted 65+ more of these> 
Converting [''] 
Converting [''] 
Converting [''] 
[[1], [2], [3.0], [0.3], [0.3], [2000.0], [-20.0], [0.022256], [''], [''], [''], [''], [''], [''], [''], [''], [''], [''], [''], [''], ['']] 
>>> 

虽然我继续寻找&学习,我想张贴问题,以亲的会帮助我和其他人。

问候,

比尔

回答

2

I can't yet notice any typo's so

...哎呀...

(?P<float2>\.\d+)? 

应该

(?P<float2>\.\d+) 

问题解决了。

相关问题