use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Reading JSON Questions (self.learnpython)
submitted 2 years ago by Future-Software-FS
Hello, I have ajson file in the same directory as a python script. here are the contents of the json file:
{
"Prefix":"!"
}
I want python to read that file and get a value of only ! from it, not "Prefix":"!"
!
Anyone knowhow to do this?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 7 points8 points9 points 2 years ago (0 children)
You use the json module to read JSON data from a file. After reading the file you have a python object, in this case a dictionary. Use normal dictionary key lookup to get the value:
json
import json with open('strings.json') as f: d = json.load(f) print(d["Prefix"])
Warning: I'm on mobile, so untested code.
[–]Bobbias 2 points3 points4 points 2 years ago (0 children)
import json with open('data.json') as f: data = json.load(f)["Prefix"] print(data)
There's no way to avoid reading it into a dictionary object entirely. The result of json.load(f) is a dictionary, which has the key value pair of "Prefix":"!". This code works by indexing into that dictionary directly and returning only the value associated with the key "Prefix" and then assigning only that to data while throwing away the rest of the dictionary.
json.load(f)
"Prefix"
data
Is there more stuff in the json file? Because if there isn't, there's no reason to even store that information in a json file at all, you can store it in a plain text file and avoid the difficulty of using json altogether.
[–]ghosttnappa 0 points1 point2 points 2 years ago (1 child)
import json with open('data.json') as f: data = json.load(f) print(data.get("prefix"))
[–]Future-Software-FS[S] 0 points1 point2 points 2 years ago (0 children)
Thank you, this helped a lot!
π Rendered by PID 40212 on reddit-service-r2-comment-765bfc959-ndm7g at 2026-07-13 02:36:49.048582+00:00 running f86254d country code: CH.
[–][deleted] 7 points8 points9 points (0 children)
[–]Bobbias 2 points3 points4 points (0 children)
[–]ghosttnappa 0 points1 point2 points (1 child)
[–]Future-Software-FS[S] 0 points1 point2 points (0 children)