views:

52

answers:

3

I am looking for a method to parse readable (i.e., not binary) data files with sections.

I had been using ConfigObj to read config files (INI files?), but I ran into a problem with multi-line lists. Specifically, ConfigObj does not allow list members to contain carriage returns. In other words, the following fails to parse:

[section]
data = [(1, 0.1),
        (2, 0.2),
        (3, 0.3)]

Removing the carriage returns fixes the problem

[section]
data = [(1, 0.1), (2, 0.2), (3, 0.3)]

Obviously, I could just use this simple fix, but the readability suffers significantly when the data extends beyond a single line. Is there an alternate config-file parser that would work here?

Alternatively, are there parsers for csv files with sections? For example, something that could parse

[data1]
1, 0.1
2, 0.2
3, 0.3

[data2]
1, 0.1
2, 0.2
3, 0.3

I considered JSON files but I wasn't quite happy with the look of the data files.

NOTE: the 1, 2, 3 columns are just for illustration: it is not my intent to save row numbers.

+2  A: 

Take at look at YAML files. There is a Python module called pyyaml to read those. I find YAML to be pretty readable.

Jim Brissom
Thanks, I've come across YAML before, but never tried it out. I was able to get it up and running on a simple dataset really easily.
tsyu80
A: 

If not json, then maybe YAML? http://pyyaml.org/

Radomir Dopieralski
+1  A: 

ConfigParser is another standard library module that should let you read files like this:

[section]
data = 
     row1, 1, 2
     row2, 2, 3
     row3, 3, 4
Ned Batchelder
Thanks! You're right, ConfigParser does work as you suggest. I ended up going with YAML as suggested by Jim and Radomir (they were first :). Also, I have a couple of nit-picky annoyances with the ConfigParser interface (which is why I was using ConfigObj).
tsyu80
Fortunately ConfigParser is being worked on and improved, so your annoyances may be gone in a few months (and years before that version of python becomes popular).
Radomir Dopieralski