Reputation: 119
I'm a beginner in Python and I'm reading from a file with the content:
{"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.\""]}*
As you can see it's almost a json-file but with additional characters like: [\ .
Task: Formate content of the file so I can access separate quotes and print out random quotes.
I could try something like this
jsonfile = open(INPUT, "r")
jsonobject = json.load(jsonfile)
someString = "\n\"{quotes}\"\n".format(quotes=jsonobject["quotes"])
which will get rid of {quotes:} from the string. Though additional unwanted characters remain and I have tried using string.replace
separately and in a loop but it doesn't give me the result I want.
Example: holder = someString.replace("[\]", '')
After the formatting is done I guess I should use a loop and try the random module with random.string
?
Upvotes: 0
Views: 123
Reputation: 1121744
You have valid JSON data already. \"
is an escaped quote (so it is part of the string value), and [...]
is a JSON array (analogous to a Python list).
Just load your data as 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.
In the above demo I used the random.choice()
function to pick one of the quotes from the list at random.
The only thing missing is Marvin's lullaby, my favourite of all Marvin's utterings:
Now the world has gone to bed
Darkness won't engulf my head
I can see by infra-red
How I hate the nightNow I lay me down to sleep
Try to count electric sheep
Sweet dream wishes you can keep
How I hate the night
Upvotes: 4
Reputation: 2462
This should work as is.
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))]
Upvotes: 0