Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字典测试Q(python)_Python_File_Dictionary - Fatal编程技术网

字典测试Q(python)

字典测试Q(python),python,file,dictionary,Python,File,Dictionary,我对如何处理这个问题感到困惑。我正在做这个练习测试,这是其中一个问题。我从创建一本新词典开始。我下一步做什么 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

我对如何处理这个问题感到困惑。我正在做这个练习测试,这是其中一个问题。我从创建一本新词典开始。我下一步做什么

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'''
当我们从文本文件中取出行时,我们是否总是必须将其剥离

问题的b部分也令人困惑。它告诉我们编写一个程序,以上面描述的格式打开一个名为“students.txt”的文件,调用我们的函数来构建字典,并将字典pickle到一个名为“students.pck”的文件。我们可以假设导入了
cpickle
,并且定义了该函数
process\u students


我不知道泡菜是什么。但是我甚至不能完成第一个,所以我不知道如何继续第二个

查看用于读取数据文件的Python CSV模块,然后在处理每个CSV行时,将值插入字典中

特别要注意的是:


这可能解释了
r
参数所代表的
读取器的原因。

在这项研究中,我认为你会学到更多的自己解决问题的方法,而不是阅读别人的解决方案。好的起点是查看用于解析输入文件的示例,以及python帮助中关于如何操作的教程部分

当我们从文本文件中取出行时,我们是否总是必须将其剥离


这是摆脱性格的简单方法。我想你不需要它们:)

显然,大多数人都不愿意给你一个完整的解决方案,因为你没有从中学到很多东西,但指南确实让事情变得相当简单。这里有一些提示

1) 你已经读了你的台词。这条线是一条线。使用字符串的split方法将其划分为组件字符串列表。例如,
“this,is,a,test”.split(“,”)=>[“this”,“is”,“a”,“test”]

2) 要访问列表的元素,请执行以下操作:

    for line in r:
        row = line.strip().split(',')
        print row
3) 字典使用起来很有趣:

mylist = [1,2,3,4]
mylist[2] # => 3

#or map the values to variable names
a,b,c,d = mylist

这些只是一些对你有帮助的提示。

这太复杂了。我不理解它,我是新手,所以除此之外的任何东西对我来说都是太多了。Python文档是最好的,启动IDLE或类似ipython或dreampie的东西,然后深入阅读教程。事实上,正如特别提到的>>除了用作分隔符的逗号外,没有其他逗号。嗯,你能不能给我一点指导,告诉我该怎么做。事实上,有人特别提到>>除了用作分隔符的逗号外,没有其他逗号。
    for line in r:
        row = line.strip().split(',')
        print row
mylist = [1,2,3,4]
mylist[2] # => 3

#or map the values to variable names
a,b,c,d = mylist
mydict = {}
mydict['things'] = []
mydict['things'].append("foo")
mydict['other_things'] = ["bar"]

mydict['things'] # => ["foo"]
mydict['other_things'] # => ["bar"]