2

I have a string like this

"{""netPrice"":251.6,""totalPrice"":299.4,""calculatedTaxes"":[{""tax"":47.8,""taxRate"":19.0,""price"":251.6,""extensions"":[]}],""taxRules"":[{""taxRate"":19.0,""percentage"":100.0,""extensions"":[]}],""positionPrice"":232.0,""rawTotal"":299.4,""taxStatus"":""net""}"

and i need it to be an dict like {'netPrice': 251.6, 'totalPrice':299.4, and so on} but since it hast the double quotes eval and json doesnt work for this. I import that string out of a csv file with

with open('order.csv', 'r') as csv_datei:
    for row in csv_datei:

so i cant get it in a cleaner format as far as i know. (the String is the row)

How do I convert it into a dict?

5
  • 2
    That is not a valid string literal. How did you get it? Can you print(repr(str)) to check exactly what is in it? It looks like it might be a JSON string. If so, use json.loads on it.
    – Amadan
    Commented Jul 7, 2022 at 8:40
  • If that is an exact representation of a row in your order.csv file, as it seems like the result of encoding JSON into CSV, use csv.reader to read it, then json.loads to parse it.
    – Amadan
    Commented Jul 7, 2022 at 8:41
  • i tried json.loads but this also doesnt convert it into a dict. here comes the error json.decoder.JSONDecodeError: Extra data: line 1 column 4 (char 3)
    – Erik
    Commented Jul 7, 2022 at 8:44
  • i got the string out of an csv file which i got out of an database.
    – Erik
    Commented Jul 7, 2022 at 8:44
  • 1
    and if i run print(repr(str)) i get '"{""netPrice"":251.6,""totalPrice"":299.4,""calculatedTaxes"":[{""tax"":47.8,""taxRate"":19.0,""price"":251.6,""extensions"":[]}],""taxRules"":[{""taxRate"":19.0,""percentage"":100.0,""extensions"":[]}],""positionPrice"":232.0,""rawTotal"":299.4,""taxStatus"":""net""}"\n'
    – Erik
    Commented Jul 7, 2022 at 8:46

1 Answer 1

1

As suggested in comments, this seems to be a JSON that has been placed inside CSV. Standard CSV surrounds string values with double quotes ("..."), and uses double double-quotes ("") to denote a double-quote character (") inside a string.

As it is doubly encoded, pass it through both of the relevant parsers:

import csv
import json

with open('order.csv', 'r') as csv_datei:
    for row in csv.reader(csv_datei):
        data = json.loads(row[0])
        print(data)

# => {'netPrice': 251.6, 'totalPrice': 299.4, 'calculatedTaxes': [{'tax': 47.8, 'taxRate': 19.0, 'price': 251.6, 'extensions': []}], 'taxRules': [{'taxRate': 19.0, 'percentage': 100.0, 'extensions': []}], 'positionPrice': 232.0, 'rawTotal': 299.4, 'taxStatus': 'net'}
3
  • when i run it with json.loads I get the following error: ``` json.decoder.JSONDecodeError: Unterminated string starting at: line 1 column 1 (char 0)```
    – Erik
    Commented Jul 7, 2022 at 8:51
  • print(row) before data = ... to see exactly what data you are having a problem with. I checked that it works with the row you posted.
    – Amadan
    Commented Jul 7, 2022 at 8:52
  • it works thanks i didnt add the csv.reader befor my csv_datei
    – Erik
    Commented Jul 7, 2022 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.