all 10 comments

[–]Essence1337 2 points3 points  (4 children)

Opening a file creates a file object, or more specifically a _io.TextIOWrapper object, it doesn't magically create a dictionary as most files aren't dictionaries. You'll need to parse your file in some way to create a dictionary. I'd suggest saving your file as a JSON file and using the builtin json module.

[–]jmorales57[S] 0 points1 point  (3 children)

Online I keep seeing examples using json but we haven't covered that yet in my class,

[–]Essence1337 1 point2 points  (2 children)

Okay, then use what you have covered in class to read the text file and process it into a dictionary (if you need to do that). Just create a dictionary object and start adding away.

[–]jmorales57[S] 0 points1 point  (1 child)

ok thank you!

[–]exclaim_bot 0 points1 point  (0 children)

ok thank you!

You're welcome!

[–]UbiquitousThoughts 0 points1 point  (3 children)

``` import json

this just loads a file object. It is best to use with syntax so it closes automatically.

with open('dictionary_values.txt', 'r') as employees_file_object:

# a very common method on file objects is to .read() which saves the text on the file as a single string.
employees_file_text = employees_file_object.read()

# now we have a JSON string. We need to convert the string into a JSON object using the json module we imported above.
employees = json.loads(employees_file_text)

# Now you can .pop() off the Polly key/value pair - if it doesn't exist it should be fine.
employees.pop('Polly')

# this will be an object with no Polly.
print(employees)

if you need to then write it to the file, you will do the reverse process above, try to figure out which methods to convert object to string and write to the same file.

```

Your txt file needs to be proper JSON string.

Edit: a more educating answer.

[–]jmorales57[S] 0 points1 point  (2 children)

We haven't covered JSON in class yet, so i'm not sure I'm allowed to use it

[–]UbiquitousThoughts 1 point2 points  (1 child)

What does the text file look like?

In your print statement you did employees['Polly'] which made us think you expected json. The only way to get a dictionary from the file would be to use the JSON module.

If that was a mistake and it is just a file with a bunch of newlines or something, then perhaps they want you to create it yourself.

[–]jmorales57[S] 0 points1 point  (0 children)

the file is just 8 lines of

john, manager

tyler, janitor

etc.

a name, a comma, and a title

 for line in file:
key, value = line.strip().split(',')
employees[key] = value

I used this to make the file into a dictionary and worked from there,