ConfigNumParser ValueError:int()的基数为10的无效文字:''

时间:2013-02-21 20:49:09

标签: python parsing numpy scipy pyparsing

此代码几乎完全是从scipy.org cookbook recipe输入的,我还不能注意到任何拼写错误,所以代码可能是oud?为什么这段代码正确解析数字但是KeyWord()和QuotedString()方法失败?

    #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:对于带有基数为10的int()的无效文字:''所以我添加了一个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], [''], [''], [''], [''], [''], [''], [''], [''], [''], [''], [''], [''], ['']]
>>>

我继续搜索&amp;学习,我认为向专业人士发布问题将有助于我和其他人。

此致 比尔

1 个答案:

答案 0 :(得分:2)

  

我还没有注意到任何错字

...噢...

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

应该是

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

修正了它。