Parsing error for ConfigParser

Philip Semanchuk philip at semanchuk.com
Thu Sep 23 17:55:26 EDT 2010


On Sep 23, 2010, at 1:22 PM, Andrew Z. wrote:

> Is there a way to parse RealPlayer's realplayerrc in Python?  I need
> to support Python 2.5 - 2.7
> 
> Example code
> 
> import urllib2
> import ConfigParser
> f = urllib2.urlopen('http://pastebin.com/download.php?i=N1AcUg3w')
> config = ConfigParser.RawConfigParser()
> config.readfp(f)
> 
> 
> Error
> Traceback (most recent call last):
>  File "test_config_parser_real_player.py", line 6, in <module>
>    config.readfp(f)
>  File "/usr/lib/python2.6/ConfigParser.py", line 305, in readfp
>    self._read(fp, filename)
>  File "/usr/lib/python2.6/ConfigParser.py", line 510, in _read
>    raise e
> ConfigParser.ParsingError: File contains parsing errors: <???>
>        [line 31]: '%0aCopyright (c) 1995-2000 Macromedia, Inc. All
> rights reserved.\r\n'
>        [line 34]: '%0a\r\n'

Hi Andrew,
Hopefully someone familiar with the topic of RealPlayer file formats will step in, but while you're waiting here's my $.02. 

Looks like this is an INI file, and since there's no defined standard for INI files, it's hard for anyone to write a parser that is guaranteed to work with all INI files. 

In this particular case you've got a multiline entry in the INI file which apparently ConfigParser doesn't like. One approach would be to subclass ConfigParser and massage the lines it doesn't like into something ConfigParser finds more palatable. Or if you're not interested in those lines, parse the error message, delete the offending lines and re-run the INI file through ConfigParser.

Good luck
Philip











More information about the Python-list mailing list