Click here to Skip to main content
14,828,290 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i append data from a .txt file into a dictionary?

My .txt file looks like this:

"":"Who are you?"
"A human.":"Do you have skin?"
"Do you have skin?":"Yes I do."

I currently have this as a .py file but it doesn't work:

actions1 = open('file path').read
actions = {actions1}

What I have tried:

I have tryed to do what I have posted above
Updated 16-Feb-21 1:13am

1 solution

You should read each line of the file one by one, rather than all in one go. Then for each line of text you need to separate the two parts either side of the colon. You can then use those two parts to add to your dictionary.

See 7. Input and Output — Python 3.7.9 documentation[^] and 5. Data Structures — Python 3.7.9 documentation[^].
Maciej Los 16-Feb-21 7:43am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900