ROLLIN40SCRIPS
ROLLIN40SCRIPS

Reputation: 21

Function that writes information into a text file python

So I got a program that made a nested dictionary with this code:

persons={}

def read_person_data(filename):
    with open(filename,'r') as document:
        for line in document:
            name, age, gender = line.strip().split(', ')
            person={'Age': age, 'Gender': gender}
            persons[name]=person
    return persons

persons=read_person_data(sys.argv[1])    

print(persons)

When I print I get this nice dictionary:

In [1]: run people_dict.py persons.txt persons.txt
{'John': {'Age': '55', 'Gender': 'Male'}, 
'Toney': {'Age': '23', 'Gender': 'Male'}, 
'Karin': {'Age': '42', 'Gender': 'Female'}, 
 Cathie': {'Age': '29', 'Gender': 'Female'}, 
'Rosalba': {'Age': '12', 'Gender': 'Female'}, 
'Nina': {'Age': '50', 'Gender': 'Female'}, 
'Burton': {'Age': '16', 'Gender': 'Male'}, 
'Joey': {'Age': '90', 'Gender': 'Male'}}

Now I want to write a function for example:

def write_person_data(data_dict,filename): where the first argument is a nested dictionary like the one I made above, and the second argument is a file name. I want the function to write the information in the dictionary to the specified file, in this type of format: How can I do it?

John, 55, Male
Toney, 23, Male
Karin, 42, Female
Cathie, 29, Female
Rosalba, 12, Female
Nina, 50, Female
Burton, 16, Male
Joey, 90, Male

Upvotes: 0

Views: 227

Answers (2)

Andrej Kesely
Andrej Kesely

Reputation: 195438

You can use this example to write the dictionary to file:

persons = {'John': {'Age': '55', 'Gender': 'Male'}, 
'Toney': {'Age': '23', 'Gender': 'Male'}, 
'Karin': {'Age': '42', 'Gender': 'Female'}, 
'Cathie': {'Age': '29', 'Gender': 'Female'}, 
'Rosalba': {'Age': '12', 'Gender': 'Female'}, 
'Nina': {'Age': '50', 'Gender': 'Female'}, 
'Burton': {'Age': '16', 'Gender': 'Male'}, 
'Joey': {'Age': '90', 'Gender': 'Male'}}

def write_person_data(data_dict,filename):
    with open(filename, 'w') as f_out:
        for k, v in data_dict.items():
            print('{}, {}, {}'.format(k, v['Age'], v['Gender']), file=f_out)

write_person_data(persons, 'data.txt')

Writes data.txt:

John, 55, Male
Toney, 23, Male
Karin, 42, Female
Cathie, 29, Female
Rosalba, 12, Female
Nina, 50, Female
Burton, 16, Male
Joey, 90, Male

Note: You can use csv module for the task too.

Upvotes: 1

Mike67
Mike67

Reputation: 11342

You can use list comprehension to get the right format:

d = {'John': {'Age': '55', 'Gender': 'Male'}, 
'Toney': {'Age': '23', 'Gender': 'Male'}, 
'Karin': {'Age': '42', 'Gender': 'Female'}, 
'Cathie': {'Age': '29', 'Gender': 'Female'}, 
'Rosalba': {'Age': '12', 'Gender': 'Female'}, 
'Nina': {'Age': '50', 'Gender': 'Female'}, 
'Burton': {'Age': '16', 'Gender': 'Male'}, 
'Joey': {'Age': '90', 'Gender': 'Male'}}

txt = '\n'.join([', '.join((k,d[k]['Age'],d[k]['Gender'])) for k in d])

with open('data.txt','w') as f: 
   f.write(txt)

Output

John, 55, Male
Toney, 23, Male
Karin, 42, Female
Cathie, 29, Female
Rosalba, 12, Female
Nina, 50, Female
Burton, 16, Male
Joey, 90, Male

Upvotes: 0

Related Questions