1

このコードはscipy.org のクックブック レシピからほぼ正確に入力されていますが、まだタイプミスに気付かないので、コードが古くなっているのでしょうか? このコードが数値を正しく解析するのに、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: invalidliteral for int() with base 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], [''], [''], [''], [''], [''], [''], [''], [''], [''], [''], [''], [''], ['']]
>>>

私は検索と学習を続けていますが、質問をプロに投稿すると、私や他の人に役立つと思いました.

よろしく、 ビル

4

1 に答える 1

2

まだタイプミスに気付かないので

...おっと...

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

する必要があります

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

それはそれを修正しました。

于 2013-02-21T21:01:45.233 に答える