簡體   English   中英

格式化幾乎一個json文件

[英]Formatting almost a json-file

我是Python的初學者,正在讀取包含以下內容的文件:

{“ quotes”:[“我可以計算出您的生存機會,但是您不喜歡它。”,“我會給您建議,但您不會聽。沒有人願意。”,“我很痛,所以我是。”,“我已經看過了。那是垃圾。(關於亞瑟發現的麥哲倫日落很壯觀)”,“不是有人在乎我在說什么,而是餐廳在宇宙的另一端。 “,“我認為您應該知道我感到非常沮喪。”,“我的幸福感,”他補充說,“您可以放入火柴盒中,而無需先進行比賽。”,“亞瑟:\\ “馬文,有什么主意嗎?”馬文:“我有上百萬個主意。 他們都指向一定的死亡。\\“”,“ \\”怎么了?\\“ [問福特。] \\”我不知道,“馬文說,”我從未去過那里。“”, “馬文:\\”據粗略估計,我比你聰明三百億倍。 讓我給你舉個例子。 想一個數字,任何數字。\\“ Zem:\\” Er,五個。\\“ Marvin:\\”錯了。 您看到了嗎?\\“”,“ Zaphod:\\” Trillian可以嗎,我正試圖有尊嚴地死。 馬文:\\“我只是想死。\\”“]} *

如您所見,它幾乎是一個json文件,但帶有其他字符,例如:[\\。

任務 :格式化文件的內容,以便我可以訪問單獨的引號並打印出隨機引號。

我可以嘗試這樣的事情

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

它將擺脫字符串中的{quotes:}。 盡管還保留了其他不需要的字符,並且我嘗試單獨使用並在循環中使用string.replace ,但它沒有給我想要的結果。

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

格式化完成后,我想我應該使用循環並嘗試使用random.string的random模塊?

你有有效的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()函數從列表中隨機選擇一個引號。

唯一缺少的是馬文的搖籃曲,我最喜歡馬文所有的話:

現在世界已經睡覺了
黑暗不會吞沒我的頭
我可以通過紅外線看到
我如何討厭夜晚

現在我要睡了
嘗試數數電羊
祝你夢wishes以求
我如何討厭夜晚

這應該按原樣工作。

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))]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM