0

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!

Jake
  • 77
  • 6
  • you may find mutli form toolkit helpful, example use in [link](https://stackoverflow.com/questions/33369306/parse-multipart-form-data-received-from-requests-post) – kamster Feb 05 '22 at 21:57

1 Answers1

1

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("---", "")
Cresht
  • 1,020
  • 2
  • 6
  • 15
  • If I get you right, the code should then look like this: with open('test.ini', 'r') as f: config_string = '[dummy_section]\n' + f.read() config_string = config_string.replace("---", "") config = configparser.ConfigParser() test = config.read_string(config_string) But now I'm getting only "None" as a result.. – Jake Feb 05 '22 at 21:58
  • 1
    Yes, that looks right – Cresht Feb 05 '22 at 22:01
  • So, your answer works but I'm still not getting an answer unfortunately.. Thank you anyways! :) – Jake Feb 05 '22 at 22:11