I want to preserve the dictionary function when I save the dictionary type data once as text data and call it in another file.
For example
{
'Name': ['John', 'Kawakami', 'Ueda', 'Paul'],
'Gender': ['woman', 'woman', 'woman', 'woman'],
'E-mail address': ['[email protected]', '[email protected]', '[email protected]', '[email protected]']
}
. When I save this data as txt data, I want to use it in another file.
However, if it is called as text data, it will be treated as a list or character string and will not function as a dictionary.
Is there a way to use it as a dictionary?
Try it,
def write_txt (type_of_dictionary):
with open ('sample_w2.txt', 'w') as f:
for k, v in type_of_dictionary.items ():
if sorted (type_of_dictionary.keys ()) [0] == k:
print ('run')
f.write ('{' + '{0}'. format (k) + ':' + str (v) + '\ n')
f.write ('{0}'. format (k) + ':' + str (v) + '\ n')
if sorted (type_of_dictionary.keys ()) [len (type_of_dictionary.keys ())-1] == k:
print ('end')
f.write ('{0}'. format (k) + ':' + str (v) + '}')
Now, the text data is saved as a dictionary-like shape, but because it is written as a string,
When reading with another file and specifying key, it is said that key does not exist in the list.
By the way,
ast library literal_eval ()
Was used but didn't work.
-
Answer # 1
Related articles
- python - i want to add an article category sorting function on the django blog
- python 3x - i want to shorten the python function
- processing using the len function when an integer value is obtained from python standard input
- python - i want to create a function that finds the standard deviation sigma from x, y
- python - how to output the key and value in the dictionary in any form
- python - how to store the characters in the list in the dictionary
- please explain the function using the python dictionary
- python - i want to get rid of the error that the function is defined but not defined
- python - i'm looking for a method like the countif function in excel for pywin32
- python - i want to know the type of softmax function in pytorch
- python - pass a variable to the websocket on_message function
- python - arguments are passed even though the function inside the function is not in parentheses
- i want to create a function in python that all functions without class go through
- python - i want to remove whitespace in dictionary values
- python - conditional statement when dictionary type data is not passed in django
- python - out of range of predict function used in image recognition (?)
- python - array elements cannot be used as function arguments ??
- python scraping consecutive urls and saving as csv
- i don't know the basic usage of the python generator function
- i want to create a function that calculates the absolute value in python
Please save in json format.
You can easily save it as a dictionary, and you can use it as it is.
If you specify ensure_ascii = False for json.dump, Japanese can be output.