格式化几乎json文件

时间:2016-04-08 17:01:44

标签: python json python-3.x

我是Python的初学者,我正在阅读包含以下内容的文件:

  

{"引用":["我可以计算你的生存机会,但你不会喜欢它。","我' d给你建议,但你不会听。从来没有人这样做过。","我很痛苦,因此我是。","我已经看过了。这是垃圾。 (关于Arthur发现壮丽的Magrathean日落)",#34;并不是说有人关心我说的话,但餐厅就在宇宙的另一端。","我认为你应该知道我感到非常沮丧。","我的幸福能力,\"他补充道,\"你可以放入火柴盒而不先拿出比赛。","亚瑟:\"马文,任何想法?\"马文:\"我有一百万个想法。他们都指出了某种死亡。\""," \"什么是死亡?\" [问福特。] \"我不知道,\"马文说,\"我从未去过那里。\"","马文:\"我粗略估计比三十亿倍您。让我给你举个例子。想想一个数字,任何数字。\" Zem:\"呃,五个。\"马文:\"错了。你看到了吗?""," Zaphod:\"它可以Trillian,我试图有尊严地死去。马文:\"我只是想死。\""]} *

你可以看到它几乎是一个json文件,但有其他字符,如:[\。

任务:文件的Formate内容,以便我可以访问单独的引号并打印出随机引号。

我可以尝试这样的事情

jsonfile = open(INPUT, "r")
jsonobject = json.load(jsonfile)
someString = "\n\"{quotes}\"\n".format(quotes=jsonobject["quotes"])

将从字符串中删除{quotes:}。虽然还有其他不需要的字符,但我已尝试分别在循环中使用string.replace,但它并没有给我我想要的结果。

示例:holder = someString.replace("[\]", '')

格式化完成后我想我应该使用循环并尝试使用random.string的随机模块?

2 个答案:

答案 0 :(得分:4)

您已有有效的JSON数据\"是一个转义引号(因此它是字符串值的一部分),而[...]是一个JSON 数组(类似于Python列表)。

只需将数据加载为JSON:

>>> import json
>>> jsondata = r'''{"quotes":["I could calculate your chance of survival, but you won't like it.","I'd give you advice, but you wouldn't listen. No one ever does.","I ache, therefore I am.","I've seen it. It's rubbish. (About a Magrathean sunset that Arthur finds magnificent)","Not that anyone cares what I say, but the Restaurant is on the other end of the universe.","I think you ought to know I'm feeling very depressed.","My capacity for happiness,\" he added, \"you could fit into a matchbox without taking out the matches first.","Arthur: \"Marvin, any ideas?\" Marvin: \"I have a million ideas. They all point to certain death.\"","\"What's up?\" [asked Ford.] \"I don't know,\" said Marvin, \"I've never been there.\"","Marvin: \"I am at a rough estimate thirty billion times more intelligent than you. Let me give you an example. Think of a number, any number.\" Zem: \"Er, five.\" Marvin: \"Wrong. You see?\"","Zaphod: \"Can it Trillian, I'm trying to die with dignity. Marvin: \"I'm just trying to die.\""]}'''
 >>> data = json.loads(jsondata)
 >>> data
 {'quotes': ["I could calculate your chance of survival, but you won't like it.", "I'd give you advice, but you wouldn't listen. No one ever does.", 'I ache, therefore I am.', "I've seen it. It's rubbish. (About a Magrathean sunset that Arthur finds magnificent)", 'Not that anyone cares what I say, but the Restaurant is on the other end of the universe.', "I think you ought to know I'm feeling very depressed.", 'My capacity for happiness," he added, "you could fit into a matchbox without taking out the matches first.', 'Arthur: "Marvin, any ideas?" Marvin: "I have a million ideas. They all point to certain death."', '"What\'s up?" [asked Ford.] "I don\'t know," said Marvin, "I\'ve never been there."', 'Marvin: "I am at a rough estimate thirty billion times more intelligent than you. Let me give you an example. Think of a number, any number." Zem: "Er, five." Marvin: "Wrong. You see?"', 'Zaphod: "Can it Trillian, I\'m trying to die with dignity. Marvin: "I\'m just trying to die."']}
>>> from pprint import pprint
>>> pprint(data)
{'quotes': ["I could calculate your chance of survival, but you won't like it.",
            "I'd give you advice, but you wouldn't listen. No one ever does.",
            'I ache, therefore I am.',
            "I've seen it. It's rubbish. (About a Magrathean sunset that "
            'Arthur finds magnificent)',
            'Not that anyone cares what I say, but the Restaurant is on the '
            'other end of the universe.',
            "I think you ought to know I'm feeling very depressed.",
            'My capacity for happiness," he added, "you could fit into a '
            'matchbox without taking out the matches first.',
            'Arthur: "Marvin, any ideas?" Marvin: "I have a million ideas. '
            'They all point to certain death."',
            '"What\'s up?" [asked Ford.] "I don\'t know," said Marvin, "I\'ve '
            'never been there."',
            'Marvin: "I am at a rough estimate thirty billion times more '
            'intelligent than you. Let me give you an example. Think of a '
            'number, any number." Zem: "Er, five." Marvin: "Wrong. You see?"',
            'Zaphod: "Can it Trillian, I\'m trying to die with dignity. '
            'Marvin: "I\'m just trying to die."']}
>>> import random
>>> print(random.choice(data['quotes']))
I've seen it. It's rubbish. (About a Magrathean sunset that Arthur finds magnificent)
>>> print(random.choice(data['quotes']))
I ache, therefore I am.

在上面的演示中,我使用random.choice() function随机选择列表中的一个引号。

唯一缺少的是马文的摇篮曲,我最喜欢马文的所有说法:

  

现在世界已经上床了   黑暗不会吞没我的头脑   我可以通过红外线看到   我多么讨厌这个夜晚

     

现在我躺下睡觉了   试着计算电羊   甜蜜的梦想,你可以保持   我多么讨厌这个夜晚

答案 1 :(得分:0)

这应该按原样运作。

import json
import random
file = open(<path to your file>,'r')
i = json.load(file)
#print a random quote
print i['quotes'][int(random.randrange(0,len(i['quotes'])-1))]