I have multiple dictionaries in a doc.txt file, for example:
Dic1={ "1" : "Value1" , "2" : "Value2" , "3" : "Value3"}
Dic2={ "1" : "Value4" , "2" : "Value5" , "3" : "Value6:}
...
Dic9={ "1" : "Value10" , "2" : "Value11" , "3" : "Value12:}
The goal is to read the file and split it into 9 dictionaries so I can access them.
CodePudding user response:
You can do this:
import json
dictionaries = []
with open(filename) as file:
for line in file:
dictionaries.append(json.loads(line[5:]))
And use your dictionnary like this (ex: for Dic1):
if dictionaries[1]["1"] == "Value1":
# Do something
if you want more than 9 dict, you will need to name your dict with a constant number of character for example Dic003, Dic245, Dic034 etc. and change the "5" to correspond to the lenght of your names.
But this isn't the best way of doing that if you are the one that is creating the file.
A better way is to save the file with :
import json
data = [Dic1, Dic2, Dic3, Dic4, Dic5, Dic6, Dic7, Dic8, Dic9]
with open(filename, 'w') as f:
json.dump(data, f)
And read it like this :
with open(filename) as f:
dictionaries = json.load(f)
CodePudding user response:
change your file like that:
{
"1": "Value1",
"2": "Value2",
"3": "Value3",
},
{
"1": "Value4",
"2": "Value5",
"3": "Value6",
},
and then use ast module:
import ast
with open('doc.txt', 'r') as file:
list = ast.literal_eval(f'[{file.read()}]')
print(list)
# output: [{'1': 'Value1', '2': 'Value2', '3': 'Value3'}, {'1': 'Value4', '2': 'Value5', '3': 'Value6'}]
now you will have a list of all your dictionaries and you can access to them by index like that:
dict1 = list[0]
dict2 = list[1]
print(dict1['1'], dict2['1'])
# output: Value1 Value4
hope that helps
CodePudding user response:
Since you "control" the contents of the input file:
test.txt (with slight corrections):
Dic1={ "1" : "Value1" , "2" : "Value2" , "3" : "Value3"}
Dic2={ "1" : "Value4" , "2" : "Value5" , "3" : "Value6"}
Dic9={ "1" : "Value10" , "2" : "Value11" , "3" : "Value12"}
You could technically do
with open("test.txt", "r") as file_in:
for line in file_in:
exec(line)
print(Dic1)
However, this is of course inherently dangerous but you can determine if that is acceptable.
A similar approach with the same risks might be to rename the file to have a .py extension then import it.
import test
print(test.Dic1)
A potentially safer alternative might be to lightly parse the data and use globals()
import json
with open("test.txt", "r") as file_in:
for line in file_in:
varname, value = line.split("=")
globals()[varname] = json.loads(value)
print(Dic1)
However, if possible, I would recommend you reshape the input format of the file (since you control it) and make it a nested dictionary like:
test_reshaped.txt:
{
"Dic1": {"1" : "Value1" , "2": "Value2" , "3": "Value3"},
"Dic2": {"1" : "Value4" , "2": "Value5" , "3": "Value6"},
"Dic9": {"1" : "Value10" , "2": "Value11" , "3": "Value12"}
}
Then read it as:
import json
with open("test_reshaped.txt", "r") as file_in:
data = json.load(file_in)
print(data["Dic1"])
