There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
In this lesson, data_payload is a dictionary wrapped in [] - a list of a dictionary. Isn’t JSON like an object and uses just {}?
So, we make a JSON file out of a dictionary in a list…what if we wanted to further convert this list to an actual dictionary? I tried
import json
with open('data.json') as data_json:
data = json.load(data_json)
print(dict(data))
#Expected result:
>>{'interesting message': 'What is JSON? A web application\'s little pile of secrets.',
'follow up': 'But enough talk!'}
#Actual result:
>>> {'interesting message': 'follow up'}
Hey guys! My question is actually about the next project, the medical data project linked at the bottom. I didn’t know the best forum to post a question, so I backed up to here.
Good Day! I am Laura and am trying to learn data science to make a career change. I do not have previous tech experience other than working through some MDN courses on HTML and javascript, as well as ~15% of the Full-Stack program through codacademy. I am at the Medical Data group project and am really stuck. Right off the bat, I am having trouble understanding how to import my CSV file.
Can someone explain? Thank you! Below is my initial code and the error. Help!
import csv
ages =
sexes =
bmis =
num_children =
smoker_statuses =
regions =
insurance_charges =
def create_list(lst, csv_file, column_name):
with open(“C:\Users\florence\Documents\python-portfolio-project-starter-files\insurance.csv”) as csv_info:
csv_dict = csv.DictReader(csv_info)
for row in csv_dict:
lst.append(row[column_name])
return lst
The error returned:
File “C:\Users\florence\AppData\Local\Temp/ipykernel_16848/3400907934.py”, line 2
with open(“C:\Users\florence\Documents\python-portfolio-project-starter-files\insurance.csv”) as csv_info:
^
SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape
In a normal string Python uses the backslash \ as an escape character so it will be interpreted in an unexpected way in your code (if you’re unsure try just using that string to assign to a variable or print and I think you’ll get the same error, I don’t think it’s from the file itself).
You can either use a forward slash / and Python will interpret the path for you, you can escape each backslash with another backslash, e.g. C:\\Users\\name\\ or you can make the string itself a “raw-string” which no longer interprets the backslash as a special escape character. For a raw string put r before the string, e.g. r"raw\string with a \ is OK".
If that hasn’t covered the issue I think #get-help might be the best shout with #project as another option (shorter queries in get-help, general discussion of the project itself in projects). If you’re posting code to the forums have a look at- How do I format code in my posts? as it contains details of how to retain indentation and format the code which makes it much easier to read .
thank you for your tips. I think I’m more stuck then I thought because it doesn’t even seem to recognize my having imported csv, as it says this is not defined… I’ll try to find a better forum to ask…
I was trying to access the data inside of data.json file:
data_payload = [
{'interesting message': 'What is JSON? A web application\'s little pile of secrets.',
'follow up': 'But enough talk!'}
]
import json
with open('data.json', 'w') as data_json:
json.dump(data_payload, data_json)
with open('data.json') as data_json:
data = json.load(data_json)
print(data['interesting message'])
… but I got a error
Traceback (most recent call last):
File "script.py", line 13, in <module>
print(data['interesting message'])
TypeError: list indices must be integers or slices, not str```
Someone have the idea how to access this data?
try print(data)
or print(data[0])
I think this json creates a list containing a dictionary (from the json.load() ), just like the data_payload.
with open('data.json') as data_json:
data = json.load(data_json)
for entry in data:
for key, value in entry.items():
print("{}: {}".format(key, value))
I had the same doubt. Then I realized what we ‘dumped’ to the json file was a list (containing a dictionary), that’s why we had to access it with an INT, like @janbazant1107978602 proposed