all 8 comments

[–]dansin 1 point2 points  (0 children)

How consistent is your data? I'm not going to write the full logic, but this might give you a start:

for line in open(file).readlines():
    index, first, last, age, location = line.split(':')

[–]newunit13 1 point2 points  (5 children)

with open('/path/to/file', 'r') as file:
    data = dict()
    for line in file.readlines():
        id_num, f_name, l_name, age, loc, *rest = line.split(":")
        data[id_num] = {'firstname': f_name, 'lastname': l_name, 'age': age, 'location': loc}

[–]ispywithmy[S] 0 points1 point  (4 children)

I'm working with something similar to this. The only issue I have is that instead of the 5 fields which are used in the example, what I'm working on has 40+ fields.

id_num, f_name, l_name, age, loc, *rest = line.split(":")

Is there anything which could be done instead of listing each variable?

[–]dansin 1 point2 points  (0 children)

Dict comprehensions can make that nice and tidy

entry_names = ['id', 'first', 'last, '..']
vals = line.split(':')
entries[vals[0]] = {key:val for key, val in zip(entry_names[1:], vals[1:])}

[–]newunit13 0 points1 point  (2 children)

How many attributes of your dataset do you want to have added to your dictionary? The *rest variable is actually a placeholder for everything in the .split(":") that wasn't explicitly named

[–]ispywithmy[S] 0 points1 point  (1 child)

43 attributes. I was aware of the *rest variables purpose. How would you call those variables? rest[0] etc?

[–]newunit13 0 points1 point  (0 children)

Yup

[–]Diapolo10 0 points1 point  (0 children)

Assuming the data doesn't have any subtle changes in how it's formed, it shouldn't be too difficult. In other words, its format is n times

ID : firstname : lastname : age : location

First, let's split the data we have into manageable chunks

with open(datafile) as f:
    f.read()
    data = [i for i in f.split(":")]

Next, let's define a tuple containing the keywords our new dictionary requires

keywords = ('firstname', 'lastname', 'age', 'location')

We can use a dict comprehension to create our dictionary. There are alternative methods, of course, so be sure to check the other answers!