below is my python code
r = requests.get("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&channelId="+CHANNELID+"&order=date&key="+DEVELOPER_KEY)
json = r.json()
items = json.get("items")
videos = []
for x in items:
title = x["snippet"]["title"]
videoId = x["id"]["videoId"]
channelTitle = x["snippet"]["channelTitle"]
cam_thumbnails = x["snippet"]["thumbnails"]["medium"]["url"]
publishedAt = x["snippet"]["publishedAt"]
data = { "title" : title,
"videoId" : videoId,
"channelTitle" : channelTitle,
"cam_thumbnails" : cam_thumbnails,
"publishedAt" : publishedAt,
}
videos.append(data)
print json.dumps(videos) # this code cause problem
I inserted 'dict' to 'list' and then called json.dumps() But, error was arised error messege is 'dict' object has no attribute 'dumps'
What is problem?, and How can I solve this problem?
The Python "AttributeError: 'dict' object has no attribute 'append'" occurs when we try to call the append() method on a dictionary. To solve the error, use bracket notation to add a key-value pair to a dict or make sure to call the append() method on a list.
json. dump() method used to write Python serialized object as JSON formatted data into a file. json. dumps() method is used to encodes any Python object into JSON formatted String.
The json. dumps() method allows us to convert a python object into an equivalent JSON object. Or in other words to send the data from python to json. The json. dump() method allows us to convert a python object into an equivalent JSON object and store the result into a JSON file at the working directory.
A fp is a file pointer used to write JSON formatted data into file. Python json module always produces string objects, not bytes objects, therefore, fp.
Previously, you must have imported the json module, writing import json
, which creates a variable in your namespace, whose name is json
. Then, you do json = r.json()
, ie. you assign a new reference to the name json
, which doesn't represents the module json anymore, but, instead, the result of the r.json()
method. Thus, you can't use anymore the json module using the syntax json.function()
, because json is the result of r.json(). To resolve your problem, you must change the name of the variable named json
in your example, to, for example, json_dict
or anything else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With