Reputation: 240
I'm currently porting a PHP script to Python to learn Python. But currently I'm stuck, because I'm calling an API which always returns an ini-formatted answer something like this:
error=0
---
asin=
name=Haftnotizen
detailname=info flags Haftnotizen
vendor=GlobalNotes
maincat=Haushalt, Büro
---
I've already learnt that I can use ConfigParser
to parse ini-files.
But in this case, the ini-answer has no headings (which wouldn't be a problem with this solution) and also has these ---
separators.
My code right now is:
import configparser
with open('test.ini', 'r') as f:
config_string = '[dummy_section]\n' + f.read()
config = configparser.ConfigParser()
config.read_string(config_string)
but unfortunately it's throwing the following error message at me:
configparser.ParsingError: Source contains parsing errors: '<string>'
[line 3]: '---\n'
[line 9]: '---'
How can I ignore those lines? (Also, they're not always in the same line)
I don't know what to do next, any help would be much appreciated!
Upvotes: 0
Views: 129
Reputation: 1040
It's not pretty, but if the error string is always exactly the same you can just remove that substring from the data. You would do that with this:
config_string = config_string.replace("---", "")
Upvotes: 1