Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中,如何编写诸如½;或¼;到json文件_Python_Json - Fatal编程技术网

在Python中,如何编写诸如½;或¼;到json文件

在Python中,如何编写诸如½;或¼;到json文件,python,json,Python,Json,大宗报价 看看这里类似的问题,我想我已经解决了,而且相当简单。我试着写一行如下: “¼公斤牛肉” 到json文件。我是从一组字符串(行)中写入的 行是由带回车符的长字符串(配料)构建的 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 oni

大宗报价

看看这里类似的问题,我想我已经解决了,而且相当简单。我试着写一行如下:

“¼公斤牛肉”

到json文件。我是从一组字符串(行)中写入的

行是由带回车符的长字符串(配料)构建的

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')
我认为建议是这样做:

import json
data={}
with open('test.json', 'w', encoding="utf-8") as json_file: 
    for line in lines:
        json.dump(line, json_file)
我没有收到错误消息,但在json文件中我收到了以下注意事项:

“SyntaxError:JSON.parse:JSON数据第1行第25列的JSON数据后出现意外的非空白字符”

写入的数据为:

“\u00bckg牛肉”

以下是json文件的顶部,其中显示了:

SyntaxError:JSON.parse:JSON数据第1行第25列的JSON数据后出现意外的非空白字符 “\uuuuu名称:0:平房派”“\uuuuuuuu刮削模式”“\uuuuuu url:汤匙橄榄油”“1\u00bckg牛肉末”“2个洋葱、鱼翅

我不想本地化,我只需要处理字符分数。所以我不确定我还应该做什么?

看起来:

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

解决了这个问题。for循环似乎引入了第二个不再存在的问题。

什么是
-为什么要逐行写入文件?行是一个字符串数组。我已更新了我的问题。请共享
或其中的一个子集。从何处获得语法错误?@jz_0一行一行地创建一个json文件。您所需要做的就是
将open('test.json','w')作为f:json.dump(lines,f)