简体   繁体   中英

Python json/dictionary questions

I recently started working with json reading and writing in python and have to implement some books into a library system from a json file provided, it looks like this;

[
  {
    "author": "Chinua Achebe",
    "country": "Nigeria",
    "imageLink": "images/things-fall-apart.jpg",
    "language": "English",
    "link": "https://en.wikipedia.org/wiki/Things_Fall_Apart\n",
    "pages": 209,
    "title": "Things Fall Apart",
    "year": 1958
  },

I made this small piece of code to get my books into a python dictionary, before implementing it into my bigger system.

import json

with open('C:/Users/daann/Downloads/booksset1.json') as json_file:

    booklist = json.load(json_file)

print(booklist)

My questions are about dictionaries and how to read the data from my json in the dictionary, I have the data in a long dictionary now, but how do I read, for instance only the Authors? Or only the Names? I completly forgot and can't find it anywhere.

Another question, if I wanted to take out, for instance this first book I put in here, with author called "Chinua Achebe", is there a way to do that(take out all data concerning that book with given author name)?

Each entry in booklist is a Python dictionary, abbreviated as dict . Accessing a field in a dictionary uses the notation book[field_name] . In your case, field_name has the value "author" .

for book in booklist:
    print(book["author"]) # or any field you want

Here's some ways to iterate through your data:

booklist = [
  {
    "author": "Chinua Achebe",
    "country": "Nigeria",
    "imageLink": "images/things-fall-apart.jpg",
    "language": "English",
    "link": "https://en.wikipedia.org/wiki/Things_Fall_Apart\n",
    "pages": 209,
    "title": "Things Fall Apart",
    "year": 1958
  },

   {
    "author": "Joe Jackson",
    "country": "USA",
    "imageLink": "images/white_socks.jpg",
    "language": "English",
    "link": "https://en.wikipedia.org/wiki/white_sox",
    "pages": 500,
    "title": "My Shoes Hurt My Feet",
    "year": 1919
  },

    {
    "author": "Jane Mae",
    "country": "Canada",
    "imageLink": "images/ehhhh.jpg",
    "language": "French",
    "link": "https://en.wikipedia.org/wiki/ehhhh\n",
    "pages": 123,
    "title": "What's That Aboot",
    "year": 2000
  }]


# Get all authors in a list (might want to convert to set to remove duplicates)     
authors = [d["author"] for d in booklist] 
print (authors) 

# Find all books by author 'Chinua Achebe'
for book in booklist:
    if book['author'] == 'Chinua Achebe':
        print (book)


# Find all books later than year 1950         
for book in booklist:
    if book['year'] > 1950:
        print (book['title'], book['year'])

If you want to get books by author, you will need to build another lookup author -> books:

import collections
books_by_author = collections.defaultdict(list)
for b in booklist:
    books_by_author[d['author']].append(b)

# and then
books_by_Chinua_Achebe = books_by_author['Chinua Achebe']

This link might be helpful to start with dictionaries: https://realpython.com/python-dicts/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM