I've read the for ENCOR we just need to "6.1 Interpret basic Python components and scripts". I wonder why I see alot of high level question about Python here
Im a bit nervous about this stuff. If these are brain dumps from the actual exam, why is this stuff on here? This is not networking. ENCOR is very misleading.
Answer is C, I verified by writing the below program to test:
import json
import requests
url = "http://ip.jsontest.com"
response = requests.get(url)
print(response)
with open("ifaces.json","w") as outfile:
outfile.write(response.text)
A: wrong - python error: "argument must be str, not dict"
B: wrong - python error: "argument must be str, not Response"
C: Correct. file created as .json
D: Wrong - python error: "argument must be str, not dict"
requests.get() returns a "response" object.
write() method accepts only string datatypes as arguments, not anything else.
A: Wrong: json.loads() method takes a "string" formatted as Json as argument, and returns a dict. Trying to write a 'dict' to a file raises an exception.
B: Wrong: Trying to write the whole Response object to a file raises an exception
C: Correct: 'text' is an attribute of 'response' which is the response's body as a Json-formatted string. Thus, it can be directly written to the file.
D: Wrong: .json() method returns the response's json body to a dictionary, same as json.loads(response.text) does.
The correct answer is C.
C. with open("ifaces.json", "w") as OutFile:
OutFile.write(Response.text)
Explanation: When you make a request using the requests.get() method, the response object Response contains the server's response in text format. To save this response as a JSON-formatted file, you need to write the text attribute of the response to the file. Option C does this correctly by using OutFile.write(Response.text).
Options A and D are incorrect because they attempt to convert the response content to JSON using json.loads() or Response.json(), respectively. However, in this case, the response is already in text format and should be directly written to the file.
Option B is incorrect because it attempts to write the entire response object (Response) to the file, which will not be in valid JSON format.
Ok, I think I got it -> C. Explanation as below.
A wrong:
json.loads() is used to make a python dictionary out of a json string - this is not what we are looking for as we actually should get a json string.
B wrong:
This will provide the http response, not a text string. E.g. <Response [200]> if all good.
C correct:
Since APIs return JSON strings by default we only need to write them into a file.
Script example:
url = "http://ip.jsontest.com"
response = requests.get(url=url)
print(response.text," ",type(response.text))
Result:
{"ip": "77.112.51.84"}
<class 'str'>
D wrong:
outfile.write(response.json()) changes the type of the data from string to dictionary so we're getting an error: TypeError: write() argument must be str, not dict
This reference is the key https://requests.readthedocs.io/en/latest/user/quickstart/#json-response-content
upvoted 1 times
...
...
This section is not available anymore. Please use the main Exam Page.350-401 Exam Questions
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
Faridtnx
Highly Voted 2 years, 3 months agoSolaaa
2 years, 2 months ago[Removed]
1 year, 10 months agomgiuseppe86
1 year, 7 months agoEAC84
1 year agoPureInertiaCopy
1 year, 8 months agoattiko
Highly Voted 2 years, 6 months agoZizu007
2 years, 5 months agoColmenarez
1 year, 9 months ago[Removed]
Most Recent 11 months ago[Removed]
10 months agotafisto
1 year agoClaudiu1
1 year, 2 months agoPureInertiaCopy
1 year, 8 months agoDannyboy7
1 year, 10 months agomsstanick
1 year, 10 months agodnjJ56
2 years, 4 months agoHuntkey
2 years, 5 months agoZikosheka
2 years, 6 months agozeta99
2 years, 6 months agoWooker
2 years, 7 months agoWooker
2 years, 7 months agoTitini
2 years, 7 months agoImFran
2 years, 4 months ago