JSON转储在Python 3

时间:2018-02-16 03:01:39

标签: python json python-3.x python-jsonschema

我正在读一个文本文件然后创建一个python字典,之后我必须从该字典创建一个JSON文件。

这是一个示例文本文件:

  

伯兰州立大学的宿舍里有N名学生。他们每个人有时都想使用厨房,所以宿舍的负责人想出了一个厨房用的时间表,以避免冲突:

     

第一个学生在0时开始使用厨房,并且应该在A1时间之前完成烹饪。   第二个学生在A1时间开始使用厨房,并且应该在A2时间之前完成烹饪。   等等。

     

第N名学生在AN-1时开始使用厨房,并应在不迟于AN时完成烹饪   Berland的假期即将到来,所以今天这N名学生中的每一个都想煮一些煎饼。第i名学生需要Bi单位的时间来做饭。   学生们已经明白,可能不是所有人都能做到他们想要的一切。有多少学生可以在不违反时间表的情况下做饭?

     

输入

     

输入的第一行包含一个整数T,表示测试用例的数量。下面是T测试用例的描述。   每个测试用例的第一行包含一个表示学生数量的整数N.

     

第二行包含N个以空格分隔的整数A1,A2,...,AN,表示相应学生完成烹饪时的时刻。   第三行包含N个以空格分隔的整数B1,B2,...,BN,表示每个学生烹饪所需的时间。

     

输出

     

对于每个测试用例,输出一行,其中包含可以完成烹饪的学生人数。

     

约束

     

应包含您可能拥有的输入数据的所有约束。格式   它像:

     

1≤T≤10

     

1≤N≤10^ 4

     

0< A1< A2< ......< AN< 10 ^ 9

     

1≤Bi≤10^ 9

     

实施例

     

输入:

     

2   3

     

1 10 15

     

1 10 3

     

3

     

10 20 30

     

15 5 20

     

输出:

     

2

     

1

     

解释

     

示例案例1.

     

第一个学生有1个单位的时间 - 时刻0.这对她来说就足够了。第二个学生有9个单位的时间,但想要煮10个单位的时间,并且不适合及时。第三个学生有5个单位的时间,适合及时,因为需要只做3个单位的时间。

     

案例2。

     

每个学生有10个单位的时间,但只有第二个时间能够适应。

这是生成的JSON:

{
"uid": "abdul",
"descriptions": {
    "description0": "There are N students living in the dormitory of Berland State University. Each of them sometimes wants to use the kitchen, so the head of the dormitory came up with a timetable for kitchen's usage in order to avoid the conflicts:",
    "description1": "The first student starts to use the kitchen at the time 0 and should finish the cooking not later than at the time A1.",
    "description2": "The second student starts to use the kitchen at the time A1 and should finish the cooking not later than at the time A2.",
    "description3": "And so on.",
    "description4": "The N-th student starts to use the kitchen at the time AN-1 and should finish the cooking not later than at the time AN",
    "description5": "The holidays in Berland are approaching, so today each of these N students wants to cook some pancakes. The i-th student needs Bi units of time to cook.",
    "description6": "The students have understood that probably not all of them will be able to cook everything they want. How many students will be able to cook without violating the schedule?",
    "description7": "Input",
    "description8": "The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.",
    "description9": "The first line of each test case contains a single integer N denoting the number of students.",
    "description10": "The second line contains N space-separated integers A1, A2, ..., AN denoting the moments of time by when the corresponding student should finish cooking. ",
    "description11": "The third line contains N space-separated integers B1, B2, ..., BN denoting the time required for each of the students to cook.",
    "description12": "Output",
    "description13": "For each test case, output a single line containing the number of students that will be able to finish the cooking.",
    "description14": "Constraints",
    "description15": "Should contain all the constraints on the input data that you may have. Format it like:",
    "description16": "1 \u2264 T \u2264 10",
    "description17": "1 \u2264 N \u2264 10^4",
    "description18": "0 < A1 <  A2 < ... < AN < 10^9",
    "description19": "1 \u2264 Bi \u2264 10^9",
    "description20": "Example",
    "description21": "Input:",
    "description22": "2",
    "description23": "3",
    "description24": "1 10 15",
    "description25": "1 10 3",
    "description26": "3",
    "description27": "10 20 30",
    "description28": "15 5 20",
    "description29": "Output:",
    "description30": "2",
    "description31": "1",
    "description32": "Explanation",
    "description33": "Example case 1. The first student has 1 unit of time - the moment 0. It will be enough for her to cook. The second student has 9 units of time, but wants to cook for 10 units of time, and won't fit in time. The third student has 5 units of time and will fit in time, because needs to cook only for 3 units of time.",
    "description34": "Example case 2. Each of students has 10 units of time, but only the second one will be able to fit in time."
},
"inputs": {},
"outputs": {},
"ex_inputs": {},
"ex_outputs": {},
"miscs": {},
"constraints": {}

}

如果您在json中看到描述16-19,您可以看到它被替换为 \ u2264 ,我想让这个json的文本与文本文件中的相同

以下是我如何创建这个json:

    data = {
    'uid': userId,
    'descriptions': description,
    'inputs': inputs,
    'outputs': outputs,
    'ex_inputs': example_input,
    'ex_outputs': example_output,
    'miscs': misc,
    'constraints': constraints
}
print(data)
# Writing Finalized JSON description files
with open('/Users/abdul/PycharmProjects/d2cApi/finalized/description_' + str(fid) + '.json', 'w') as f:
    f.write(json.dumps(data, indent=4))
return json.dumps(data)

我如何实现这一目标?

请帮帮我!

提前致谢!

3 个答案:

答案 0 :(得分:2)

如何使用ensure_ascii=False

>>> d = {"descriptions": {"description16": "1 ≤ T ≤ 10"}}
>>> json.dumps(d)
'{"descriptions": {"description16": "1 \\u2264 T \\u2264 10"}}'
>>> json.dumps(d, ensure_ascii=False)
'{"descriptions": {"description16": "1 ≤ T ≤ 10"}}'

答案 1 :(得分:1)

encoding='utf-8'添加到文件的开头。

with open('/Users/abdul/PycharmProjects/d2cApi/finalized/description_' + str(fid) + '.json', 'w', encoding="utf-8") as f:

答案 2 :(得分:0)

我将上述两个答案合并为:

# Writing Finalized JSON description files
with open('/Users/abdul/PycharmProjects/d2cApi/finalized/description_' + str(fid) + '.json', 'w', encoding="utf-8")\
        as f:
    f.write(json.dumps(data, indent=4, ensure_ascii=False))
return json.dumps(data)

我在打开文件时添加了encoding="utf-8",并在将数据转储到json文件时添加了ensure_ascii=False