Reputation: 9
def process_students(r):
'''r is an open reader with no data about students: their name, cdf account, age, college and
home city. Each line has the following format:
name, cdf, age, college, city.
there are no commas other than the ones used as separators.
Return a dictionary in which each key is a college and its value is the list of cdf accounts
for students at that college'''
I am confused about how to work with this question. I'm doing this practice test and this is one of the questions. I started off by creating a new dictionary. What do I do next?
d = {}
for line in r:
line.strip()
When we take lines out from a text file, do we always have to strip it?
Part b of the question is also confusing. It tells us to write a program that opens a file called 'students.txt', in the format described above, calls our function to build the dictionary, and pickles the dictionary to a file called 'students.pck.' We may assume cpickle
has been imported and that function process_students
has been defined.
I have no idea what pickle is. But I can't even finish the first one, so I have no idea how to continue on with the second one anyway.
Upvotes: 0
Views: 90
Reputation: 3518
Obviously most people would be reluctant to give you a full solution because you don't learn so much from it, but the guide does make things fairly simple. Here's some pointers...
1) You've read in your line. The line is a string. Use the string's split method to divide into a list of component strings. E.g., "this,is,a,test".split(",") # => ["this", "is", "a", "test"]
2) To access elements of a list:
mylist = [1,2,3,4]
mylist[2] # => 3
#or map the values to variable names
a,b,c,d = mylist
3) Dictionaries are fun to use:
mydict = {}
mydict['things'] = []
mydict['things'].append("foo")
mydict['other_things'] = ["bar"]
mydict['things'] # => ["foo"]
mydict['other_things'] # => ["bar"]
These are just some hints towards the sort of things that will help you.
Upvotes: 0
Reputation: 11544
This being study, I think you'll learn more solving it yourself than reading someone's solution. Good starting points would be to look at the csv module for parsing the input file and the tutorial section in python help for how to manipulate dictionaries.
import csv
def process_students(r):
for row in csv.reader(r, delimiter=','):
print row # <-- rather than print, you should build dictionary here
Personally I'd use csv module. An alternative loop for process_students could be:
for line in r:
row = line.strip().split(',')
print row
Upvotes: 1
Reputation: 9193
When we take lines out from a text file, do we always have to strip it?
That's easy way to get rid of end-of-line character. I suppose you don't need them :)
Upvotes: 0
Reputation: 4842
Have a look at the Python CSV module for reading the data file, then as you process each csv line, insert the values into your dictionary. http://docs.python.org/library/csv.html
In particular, look at this: http://docs.python.org/library/csv.html#csv.reader
That probably explains the reader
represented by the r
param.
Upvotes: 0