2016-04-08 21 views
0

ve ben içerikli bir dosyadan okuyorum:Biçimlendirme Python içinde bir acemi neredeyse bir json dosyası

{ "tırnak": [ "Ben, hayatta kalma şansınızı hesaplayabilirsiniz ama Beğenmeyeceksin. "," Sana tavsiyede bulunurdum, ama sen dinlemezsin. Hiç kimse yapmaz. "," Ben ağrım, o yüzden ben de. "," Onu gördüm. Bu çöp. (Arthur'un muhteşem bulduğu bir Magrathean gün batımı hakkında), “Kimse benim söylediğim şeyi umursamıyor, ama Restoran evrenin diğer ucunda.”, “Sanırım bilmelisin ki çok depresif hissediyorum. "," Mutluluğumun kapasitesi, \ "diye ekledi," ilk maçları çıkarmadan bir kibrit kutusuna sığdırabilirsiniz. "," Arthur: \ "Marvin, herhangi bir fikir? \" Marvin: \ "Bir milyonum var fikirler. Hepsi belli bir ölüme işaret ediyor. \ "", "\" Ne var? \ "[Ford'a sordu.] \" Bilmiyorum, \ "dedi Marvin," Ben asla orada bulunmadım. " "Marvin: \" Ben sizden 30 milyar kat daha zeki bir tahminim var. Sana bir örnek vereyim. Bir sayı, herhangi bir sayı düşünün. \ "Zem: \" Er, beş. \ "Marvin: \" Yanlış. Görüyorsunuz? \ "", "Zaphod: \" Can Trillian, haysiyetle ölmeye çalışıyorum. Marvin: \ "Ben ölmek çalışıyorum \". "]} * Eğer neredeyse bir json dosyası var ama gibi ek karakterler ile de görebileceğiniz gibi

:. [\

Görev : dosyanın format içerik yüzden ayrı tırnak erişebilir ve rastgele tırnak yazdırabilirsiniz ben kurtulacaksınız bu

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

gibi bir şey deneyebilirsiniz

. {tırnak:}. dizesinden Th ek istenmeyen karakterler kalıyor ve ayrı ayrı ve döngü içinde string.replace kullanmayı denedim ama bana istediğim sonucu vermez.

Örnek: biçimlendirme ben bir döngü kullanmak ve random.string ile rastgele modülü denemek gerek yapıldıktan sonra holder = someString.replace("[\]", '')

?

+1

Aslında ... bu, teklif listesinin bir listesini veren geçerli bir jsondur. Örneğin, veri ["alıntılar"] [0] '' '' '' '' '' hayatta kalmayı hesaplayabilirdim, ama hoşuna gitmeyeceksin. ''. Bu '\ 'kaçış karakterlerini alırsınız çünkü json dizeleri dizeleri gömmüştür. – tdelaney

+0

BTW, dünyanın en büyük edebiyatlarından bazı alıntılar yaptığını görmek harika. – tdelaney

cevap

4

zaten numaralı geçerli JSON verisine sahipsiniz. \", çıkış karakteristiğindeki bir alıntıdır (bu nedenle dize değerinin bir parçasıdır) ve [...] bir JSON dizisi (bir Python listesine benzer) dizisidir.

Sadece JSON olarak veri yüklemek:

>>> 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. 

Ben rastgele listeden tırnak birini seçmek random.choice() function kullanılan yukarıdaki demo.

Şimdi

Dünya
Ben kızıl ötesi görebileceğiniz başım yutmak olmaz yatak
Darkness gitti: Eksik

tek şey tüm Marvin'in utterings sevdiğim, Marvin'in ninni gece Şimdi

nefret nasıl
Ben
Tatlı rüya sen ke olabilir dilek elektrik koyun saymak
deneyin uyku beni uzandı ep
nasıl Bu gibi çalışması gerekir

0

geceyi nefret ediyorum.

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))] 
İlgili konular