简体   繁体   中英

Is there a Python package to parse readable data files with sections

I am looking for a method to parse readable (ie, 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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM