python - ValueError: could not convert string to float: (buffer related?) -
condensed code
# attempt condense code while preserving parts # relevant question 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): 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() line 153-155:
if( self.parts['energy_values'] ): if( ch != '\n' ): self.data.energy_values.append( string.atof(normalize_whitespace( ch ) ) ) error:
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: not convert string 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> full input at: http://dl.dropbox.com/u/10405722/inputfile.dat
full code at: http://dl.dropbox.com/u/10405722/siesta_pdos.py
the code reads correctly first 3116 values , exits error. note same code shorter input (e.g. 3000 lines) works fine. therefore seems me buffer-related error has nothing atof.
any idea?
the documentation says string.atof is
deprecated since version 2.0: use float() built-in function.
you claim float() doesn't work, means input invalid. easy use print when finding out why doesn't work expect
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))) because had explain normalize_whitespace, means bad synonym; if called strip, every reader know did without having up.
in case don't know repr intended reduce ambiguity. example:
>>> x = '1.234' >>> print x 1.234 >>> print repr(x) '1.234' >>> print repr(float(x)) 1.234 with first print, unclear whether x numeric or string. repr, there no guessing involved.
Comments
Post a Comment