Reputation: 89
Lets say that i have a file containing information in each line and each line i want to convert in a dictionary. In the dictionary there will be one string, two integers and two lists. kinda like this: Q1 = { "string" : "name", "integer1" : 1, "integer2" : 2, "list1" : [a,b,c,d], "list2": [] } Now the file could be a txt file or not. Lets say its a txt file and every line would provide a dictionary. I am the one to write the file so I can have it in any format i want. I thought this format for each line of the file.txt: name,1,2,(a/b/c), () so the first thing is the string, the second is integer1, then integer2, list1 and list2. Each element of the dictionary separated with a comma and the elements of the lists inside parenthesis and separated with a slash ("/"). Obviously if you think there is a better idea for how the file.txt should like please let me know :) the first list i want it to be dynamic. that means some lines may contain more characters inside the parenthesis () and the second list i want it to always be empty cause i am putting things there later in my code. Each line is a dictionary and each dictionary is an element in a list i want to create. So i want to have a list of dictionaries.
I tried to open a file and play with the split function but the format of the txt file is more complicated than i thought and never got to read the file and save it into a list of dictionaries
#the file.txt should look like this:
name1,5,6,(a1/a2/a3), ()
name2,7,8,(a2/a3/a4/a5), ()
#the python code i tried:
def init():
myList=[]
with open("file.txt") as f:
for line in f:
d={}
d = dict(line.strip().split(',', 4))
myList[line]=d
return(myList)
list=[]
list=init();
Upvotes: 1
Views: 154
Reputation: 36
inside file1.txt
hey1,5,6,(a1/a2/a3), ()
hey2,7,8,(a2/a3/a4/a5), ()
You can use below code it will generate a new dictionary for each line and at last all the dictionary inside the dict_main.
index1=['string1','integer1','integer2','list1','list2']
dict_main={}
with open ('file1.txt') as f:
count=0
for line in f:
dict1={}
lst1=line.strip().split(',')
dict1[index1[0]]=lst1[0]
dict1[index1[1]]=int(lst1[1])
dict1[index1[2]]=int(lst1[2])
dict1[index1[3]]=lst1[3][1:-1].strip().split('/')
dict1[index1[4]]=[]
count+=1
dict_main['dict'+str(count)]=dict1
print(dict_main)
Result
{'dict1': {'integer2': 6, 'list2': [], 'integer1': 5, 'list1': ['a1', 'a2', 'a3'], 'string1': 'hey1'}, 'dict2': {'integer2': 8, 'list2': [], 'integer1': 7, 'list1': ['a2', 'a3', 'a4', 'a5'], 'string1': 'hey2'}}
Upvotes: 1
Reputation: 848
You can also use regex and avoid using "/" character if you want, like this:
import re
txt1 = "name1,5,6,[a1,a2,a3],[]"
regex = "([a-zA-Z0-9]*),([0-9]+),([0-9]+),\[(.*)\],\[(.*)\]"
matches = re.match(regex, txt1)
dict1 = {"string": matches.group(1), "integer1": matches.group(2), "integer2": matches.group(3),
"list1": matches.group(4).split(","), "list2": matches.group(5).split(",")}
The result being:
{'string': 'name1', 'integer1': '5', 'integer2': '6', 'list1': ['a1', 'a2', 'a3'], 'list2': ['']}
Upvotes: 0
Reputation: 588
Check this... without using any package.
with open("test.txt") as f:
lines = [ line.strip().split(",") for line in f ]
lines = [{
"string": line[0],
"integer1": int(line[1]),
"integer2": int(line[2]),
"list1": [l for l in line[3].strip("()").split("/")],
"list2": [l for l in line[4].strip("()").split("/")],
} for line in lines ]
print(lines)
Upvotes: 2
Reputation: 7186
You can use csv.DictReader
for this purpose.
With the given example file you can use it like this:
from csv import DictReader
FIELD_NAMES = ["string", "integer1", "integer2", "list1", "list2"]
with open("file_name.csv") as f:
reader = DictReader(f, fieldnames=FIELD_NAMES)
for line in reader:
# line["integer1"] = int(line["integer1"])
# ...
print(line)
# OrderedDict([('string', 'name1'), ('integer1', '5'), ('integer2', '6'), ('list1', '(a1/a2/a3)'), ('list2', ' ()')])
# OrderedDict([('string', 'name2'), ('integer1', '7'), ('integer2', '8'), ('list1', '(a2/a3/a4/a5)'), ('list2', ' ()')])
As you can see this evaluates every field as a string, so you would have to add parsing to integers and lists to this, but this should get you started.
It also returns OrderedDict
s, to, well, ensure the order of the fields. You can cast them to a normal dict with dict()
if needed.
To get a list of dicts, just do:
with open("file_name.csv") as f:
reader = DictReader(f, fieldnames=FIELD_NAMES)
print(list(reader))
# [OrderedDict([('string', 'name1'), ('integer1', '5'), ('integer2', '6'), ('list1', '(a1/a2/a3)'), ('list2', ' ()')]), OrderedDict([('string', 'name2'), ('integer1', '7'), ('integer2', '8'), ('list1', '(a2/a3/a4/a5)'), ('list2', ' ()')])]
Slightly unrelated:
list
by calling your lists list
return
s don't need their argument in parenthesis, a space after the return
is sufficient.Upvotes: 0