ValueError:无法将字符串转换为float :(缓冲区相关吗?)

时间:2013-08-16 12:13:09

标签: python xml atof

简明代码

# attempt to condense code while preserving the parts
# relevant to the question

from xml.sax import handler, make_parser

class pdosHandler(handler.ContentHandler):
    def __init__(self, data):
        self.data   = data
        self.parts  = { 'energy_values': 0 }
        self.energy_values = []

    def startDocument( self ):
        print "Reading started"

    def startElement(self, name, attrs):
        for key, val in self.parts.iteritems():
            if( name == key ):
                self.parts[key] = 1;

    def characters( self, ch ):
        if self.parts['energy_values'] :
            if ch != '\n':
                self.data.energy_values.append(float(ch.strip()))

def pdosreader(inp, data):
    handler = pdosHandler(data)
    parser = make_parser()
    parser.setContentHandler(handler)
    inFile = open(inp)
    parser.parse(inFile)

    inFile.close()

第153-155行:

if( self.parts['energy_values'] ):
    if( ch != '\n' ):
        self.data.energy_values.append( string.atof(normalize_whitespace( ch ) ) )

错误:

Traceback (most recent call last):
  File "siesta_pdos.py", line 286, in <module>
    main()
  File "siesta_pdos.py", line 278, in main
    pdosreader( args[0], data )
  File "siesta_pdos.py", line 262, in pdosreader
    parser.parse( inFile )
  File "/usr/lib/python2.7/xml/sax/expatreader.py", line 107, in parse
    xmlreader.IncrementalParser.parse(self, source)
  File "/usr/lib/python2.7/xml/sax/xmlreader.py", line 123, in parse
    self.feed(buffer)
  File "/usr/lib/python2.7/xml/sax/expatreader.py", line 207, in feed
    self._parser.Parse(data, isFinal)
  File "siesta_pdos.py", line 155, in characters
    self.data.energy_values.append( string.atof(normalize_whitespace( ch ) ) )
  File "/usr/lib/python2.7/string.py", line 388, in atof
    return _float(s)
ValueError: could not convert string to float:

inputfile中:

<pdos>
<nspin>2</nspin>
<norbitals>7748</norbitals>
<energy_values>
           -29.99997
           -29.98997
           -29.97996
           ...
           ... (3494 lines skipped)
           ...
             4.97999
             4.98999
             4.99999
</energy_values>
</pdos>

完整输入:http://dl.dropbox.com/u/10405722/inputfile.dat

完整代码:http://dl.dropbox.com/u/10405722/siesta_pdos.py


代码正确读取前3116个值,然后退出并显示错误。 请注意,输入较短的相同代码(例如3000行)可以正常工作。 因此,在我看来,与缓冲区相关的错误与atof无关。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

documentation says that string.atof

  

自2.0版开始不推荐使用:使用float()内置函数。

您声称float()不起作用,这可能意味着您的输入无效。在找出为什么某些东西不能按预期工作时,print非常容易使用

if( ch != '\n' ):
    print repr(ch), repr(ch.strip())
    print repr(normalize_whitespace(ch))
    print repr(float(ch.strip()))
    self.data.energy_values.append(string.atof(normalize_whitespace(ch)))

因为你必须解释normalize_whitespace,这意味着它是一个坏的同义词;如果你只是把它称为条带,每个读者都会知道它做了什么,而不必查阅它。

如果您不知道repr旨在减少歧义。例如:

>>> x = '1.234'
>>> print x
1.234
>>> print repr(x)
'1.234'
>>> print repr(float(x))
1.234

第一次打印时,不清楚x是数字还是字符串。有了repr,就没有猜测了。