I'm trying to build two seperate dictionaries with a file thats arranged in this format:
I need to take the name's reverse it so first name then last name, for the first dictionary I need to take the first name as the key and the other names in the first block as the values, ie, list of strings.
The second dictionary I need to again use the first name as the key ad the group or groups they belong to as the value.
I have figured out how to reverse the names using the comma to split them, however I end up with list of all the names which really doesnt help me seperate them at all.
I'm really confused as to how I can iterate over this to pull out these specific lines and then associate them as keys with other specific lines as values. Especially confused as to how I can get the first name as the key then the following names as values and then skip the blank line and start again but with the new key.
Text file Format:
The Format of the text file is exactly like this without the bullets, the desired out put diciotanries would look like this if the just contained the first block:
Person_to_friends = {'Leah Connors' : ['Frank Connors', 'Shawn Patterson', 'John Patterson']}
Persons_to_networks = {'Leah Connors' : ['Flying Club']}
When I attempted to test your code I recieved an index error
Patterson, John
Cosmo, Calvin
Patterson, Sally
Connors, Frank
Cosmo, Calvin
is supposed to be part of the second block and Connors, Frank
part of
the third with a single space in between blocks. Something is not working. I dont know why it keeps creating a space.
This is what I have so far but I think im really far off.. Please help
def load_profiles(profiles_file, person_to_friends, person_to_networks):
f = open('profiles.txt')
lines = f.readlines()
new = []
line_number = 0
while line_number < len(lines)+1:
prev_line = lines[line_number-1]
line = lines[line_number]
from_line = lines[line_number+1]
if ',' in line and ',' not in from_line and from_line.isspace() == False:
key = reverse_name(line)
elif ',' not in line and line.isspace()==False:
new.append(line.strip('\n'))
try:
person_to_networks[key].append(new)
except KeyError:
person_to_networks[key] = [new]
elif line.isspace()== True:
line_number = from_line
line_number += 1
__special__
methods. This isn't one of them though - Taymon 2012-04-03 21:09
__
, which are given special meaning by Python itself, and which programmers are explicitly and strongly discouraged from defining themselves - Taymon 2012-04-03 23:08
import itertools
import collections
person_to_networks = collections.defaultdict(set)
person_to_friends = collections.defaultdict(set)
def format_name(name):
return name.split(',')[1][1:] + ' ' + name.split(',')[0]
with open('exampletext') as f:
#cheap hack so that we detect the need for a new leader on the first line
lines = [''] + [line.strip() for line in f]
for line in lines:
if line == '':
new_leader = True
else:
if new_leader:
leader = format_name(line)
new_leader = False
else:
if ',' in line:
person_to_friends[leader].add(format_name(line))
else:
person_to_networks[leader].add(line)
print 'Person to Networks'
for p in person_to_networks:
print '%s: %r' % (p, [e for e in person_to_networks[p]])
print '\nPerson to Friends'
for p in person_to_friends:
print '%s: %r' % (p, [e for e in person_to_friends[p]])
Output:
Person to Networks
Frank Connors: ['Rowing school']
Calvin Cosmo: ['Sailing buddies', 'Dodge ball group']
Leah Connors: ['Flying Club']
Person to Friends
Frank Connors: ['Robert Connors', 'Leah Connors']
Calvin Cosmo: ['Sally Patterson', 'Shawn Patterson']
Leah Connors: ['Frank Connors', 'John Patterson', 'Shawn Patterson']
Current "exampletext":
Connors, Leah
Flying Club
Connors, Frank
Patterson, Shawn
Patterson, John
Cosmo, Calvin
Sailing buddies
Dodge ball group
Patterson, Shawn
Patterson, Sally
Connors, Frank
Rowing school
Connors, Leah
Connors, Robert
powers_of_two = [2**x for x in range(5)]
to get a list of [1, 2, 4, 8, 16]. In python, unlike most other languages, you normally use for loops over values in a list/dictionary/whatever instead of over explicit numbers. For example, for num in powers_of_two:
would loop over each number in the list that we just made - Nolen Royalty 2012-04-04 05:40
lines = [e.split(',')[1][1:] + ' ' + e.split(',')[0] if ',' in e else e for e in lines]
says "for each value in lines, if it contains a comma, do some work on it and add it to the list, if it doesn't just add it to the list how it is. - Nolen Royalty 2012-04-04 05:41