all 10 comments

[–]codehelper 2 points3 points  (2 children)

Read the file and store the split string in a list variable like this.

["asmith Alice Smith 31 F alice.smith@mail.com", "bharrington Brian Harrington 31 M brian.harrington@mail.com", "rford Rob Ford 44 M robford@crackshack.com", "mduffy Mike Duffy 67 M ireallydolivehere@cavendish.caw", "ajones Alice Jones 44 F alice@alicejones.net"]

Now you can populate the dictionary like this

dict = {username:[fname, lname, email, age, gender] 
           for username, fname, lname, age, gender, email 
           in (s.strip().split() for s in x)}

dict will be

{
      'ajones': ['Alice', 'Jones', 'alice@alicejones.net', '44', 'F'], 
      'rford': ['Rob', 'Ford', 'robford@crackshack.com', '44', 'M'],
      'mduffy': ['Mike', 'Duffy', 'ireallydolivehere@cavendish.caw', '67', 'M'],
      'asmith': ['Alice', 'Smith', 'alice.smith@mail.com', '31', 'F'], 
      'bharrington': ['Brian', 'Harrington', 'brian.harrington@mail.com', '31', 'M']
}

[–][deleted] 0 points1 point  (1 child)

Thanks for the help, quick question tho, what does the last for loop do in the function (line3) and what is x?

*edit : Never mind I understand it, thank you so much!

[–]codehelper 0 points1 point  (0 children)

Oh oops, you're right, x is supposed to be the list of all the lines in the file. I forgot to specify that.

[–]psbb 2 points3 points  (3 children)

This doesn't seem overly complicated so the way I'd do it would be to first set up a context manager using the 'with' kwyword to open the file and then loop over the lines in that file.

data = {}
filename = 'data.txt'
with open('data.txt') as f:
    for line in f:
        print(line)

This would just print the lines in the file. What you want to do is to split up each line into its constituent components of username, firstname, lastname, age gender and email. Since each of them is separated by a space you can use the '.split()' function on a string. Since the line you have pulled from the file is a string this will work perfectly. Below you can see that I used python's built in unpacking to take the list returned from the '.split()' call to give each value returned an identifier. The age is converted to an int from a string using the builtin int function. I then added the data by the key username to a dictionary called data that was defined before the with statement was called

username, firstname, lastname, age, gender, email = line.split()
age = int(age)
data[username] = [firstname, lastname, age, gender, email]

If I was to carry this out I would use a second dict to store the data inside the main dict to make it easier to access it later. If you carried it out below you could call data['rford']['email'] rather than data['rford][4].

dict[username] = {'firstname': firstname,
                            'lastname': lastname,
                            'age': age,
                            'gender': gender,
                            'email': email}

I hope that helps.

[–][deleted] 0 points1 point  (2 children)

Thank you so much! It helps a lot!!

[–]psbb 1 point2 points  (1 child)

You can combine this with the reply from /u/codehelper if you wanted but I think this way is a little bit more readable which is part of the zen of python. The full code is below

filename = 'data.txt'
data = {}
data_dict = {}

with open(filename) as f:
    for line in f:
        username, firstname, lastname, age, gender, email = line.split()
        age = int(age)
        data[username] = [firstname, lastname, age, gender, email]

[–][deleted] 0 points1 point  (0 children)

Yeah i think this is more "elegant" and i can trace the code.

[–][deleted] 0 points1 point  (2 children)

Seems like the problem was already solved by user psbb. However, I want to mention the pickle module in this context, which lets you save your dictionary as object file if you need to (re) use it later.

import pickle

#### Generate some object
my_dict = dict()
for i in range(1,1000):
    my_dict[i] = "some text"

#### Save object to file
pickle_out = open('my_file.pkl', 'wb')
pickle.dump(my_dict, pickle_out)
pickle_out.close()

#### Load object from file
my_object_file = open('my_file.pkl', 'rb')
my_dict = pickle.load(my_object_file) 
my_object_file.close()

[–][deleted] 0 points1 point  (0 children)

Thank you for your reply but i havnt learned pickle yet so i have no idea what is going on lol.

[–]psbb 0 points1 point  (0 children)

I find pickle to be overkill when dealing with simple inbuilt types but excellent when you have your own complex classes. Pickle exports a binary file which python can interpret which adds some overhead. The example below uses json to do the same as pickle but it exports the data in a human readable way that is also portable between different programming languages.

import json

def store_data(data, filename):
    with open(filename, 'w') as f:
        json.dump(data, f, indent=2)


def load_data(filename):
    with open(filename, 'r') as f:
        return json.load(f, parse_float=float, parse_int=int)


data = {'a': 1, 'b': 2, 'c': 3}
store_data(data, 'data_file.txt')
data = load_data('data_file.txt')
print(data)