2016-04-08 17 views
0

我在Python初學者,我從與內容的文件閱讀:格式幾乎是JSON文件

{「引號」:「我能計算出你的生存機會,但你會不喜歡它。「,」我會給你建議,但你不會聽,沒有人會這樣做。「,」我疼,所以我是。「,」我看過它,它是垃圾。 (關於Arthur發現宏偉的Magrathean日落)「,」不是說任何人都在乎我說的話,但餐廳在宇宙的另一端。「」我想你應該知道我感到非常沮喪。 「」我的快樂能力,「他補充說,」你可以放入火柴盒,而不必先拿出火柴。「」亞瑟:「馬文,有什麼想法?」馬文:「我有一百萬想法。 「福特問道,」我不知道,「馬文說,」我從來沒有去過那裏。「」是的, 「馬文:」我的粗略估計比你聰明三百億倍。讓我舉一個例子。想想一個數字,任何數字。「Zem:」呃,五個。「Marvin:」錯了。你看到了嗎?「」,「Zaphod:」Trillian,我想要有尊嚴地死去。張建東:\ 「我只是想死\」。「]} *

正如你可以看到它幾乎是一個JSON文件,但額外的字符,如:[\

任務 :文件的內容甲酸所以我可以訪問不同的報價,並打印出亂報價

我可以嘗試這樣的事情

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

這將擺脫。{報價:}從字符串的Th儘管額外的不必要的字符仍然存在,我已經嘗試單獨使用string.replace並在一個循環中,但它不會給我我想要的結果。

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

後格式化完成後,我想我應該使用一個循環,並嘗試隨機模塊random.string

+1

其實......這是一個有效的json,它給你一個帶引號列表的字典。例如,'data [「quotes」] [0]'是''我可以計算出你的生存期,但你不會喜歡它。「你得到這些'\'轉義字符,因爲json字符串具有嵌入字符串。 – tdelaney

+0

順便說一句,它很高興看到你有來自世界上一些最偉大的文學報價。 – tdelaney

回答

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挑選從列表中引號的一個隨機上面演示。

唯一缺少的是馬文的催眠曲,我所有的馬文的utterings的最愛:

現在世界已上牀睡覺
黑暗不會吞噬我的頭
我可以通過紅外線見
我怎麼恨晚

現在我躺下睡覺
嘗試數電氣羊
甜夢希望你能磕ep
我如何討厭夜晚

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