-1

Looking at similar questions here I thought I had this solved and it was fairly simple. I am trying to write a line like:

"¼kg beef"

To a json file. I am writing from an array of strings (lines).

lines is built from a long string(ingredients) with carriage returns.

ingredients = "__Name: 0: Cottage pie\n__scrape_schema\n__url:https://www.bbcgoodfood.com/recipes/cottage-pie\n3 tbsp olive oil\n1 ¼kg beef mince\n2 onions,finely chopped\n3 carrots,chopped\n"

lines=ingredients.split('\n')

I see the recommendation is to do something like this:

import json
data={}
with open('test.json', 'w', encoding="utf-8") as json_file: 
    for line in lines:
        json.dump(line, json_file)

I do not get an error message but in the json file I get the following note:

"SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 25 of the JSON data"

And the data written is:

" \u00bckg beef"

Here is the top of the json file showing both:

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 25 of the JSON data "__Name: 0: Cottage pie""__scrape_schema ""__url: https://www.bbcgoodfood.com/recipes/cottage-pie""3 tbsp olive oil""1 \u00bckg beef mince""2 onions,fin...

I am not trying to localize and I really need nothing more than handling the character fractions. So I am not sure what else I should do?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
jz_
  • 338
  • 2
  • 14

1 Answers1

0

It does seem that:

with open('test.json', 'w') as f: json.dump(lines, f)

solves the problem. The for loop seemed to introduce a second problem that is no longer there.

jz_
  • 338
  • 2
  • 14