Reputation: 81
I want to make a list of dicts that have the following structure,
[{'Business' : 'Attorney' , 'Website text' : '(one line from the loaded file)'}, {'Business' : 'Attorney' , 'Website text' : '(one line from the loaded file)'}, ...]
However at the moment i only have a csv file consisting of the website text.
Below i have attempted to somehow create lists of the same length as my website text file, then zip them and create a dict from that. I have been unable to find information on here when it comes to creating a dict from more that 2 lists.
with open('attorneys text.csv') as data_file:
attorneys = list(line for line in data_file)
website = []
business = []
classes = []
for l in range(len(attorneys)):
website.append("Website text")
for k in range(len(attorneys)):
business.append("Business")
for i in range(len(attorneys)):
classes.append("Attorney")
data = dict(zip((business, classes, website, attorneys)))
I am well aware of the ugliness of the try as well...
Upvotes: 0
Views: 1194
Reputation: 61
This is what i would do:
data = []
with open('attorneys text.csv') as data_file:
for line in data_file
row = { "Business": "Attorney", "Website text": line}
data.append(row)
I don t know if i understood well what you want to do
Upvotes: 0
Reputation: 71
Since the only difference is the text, you can do the following:
with open('attorneys text.csv') as data_file:
data = [{'Business':'Attorney', 'Website text':line} for line in data_file]
The 'Business':'Attorney'
pair will be the same for each dictionary, while the value corresponding to 'Website text'
will be the data from your file.
Upvotes: 2
Reputation: 726
You can use defaultdict
from collections import defaultdict
s = ["Business", "Attorney", "Website text"]
data = defaultdict(list)
data[s[0]].append(business)
data[s[1]].append(classes)
data[s[2]].append(website)
Upvotes: 0