昨天我问了这个问题,但我仍然坚持下去。我写了一个函数,目前正确读取文件,但有几个问题。将csv文件转换为字典
我遇到的主要问题是我需要以某种方式跳过文件的第一行,我不确定是否将它作为字典返回。这里是其中的一个文件的一个例子:
"Artist","Title","Year","Total Height","Total Width","Media","Country"
"Pablo Picasso","Guernica","1937","349.0","776.0","oil paint","Spain"
"Vincent van Gogh","Cafe Terrace at Night","1888","81.0","65.5","oil paint","Netherlands"
"Leonardo da Vinci","Mona Lisa","1503","76.8","53.0","oil paint","France"
"Vincent van Gogh","Self-Portrait with Bandaged Ear","1889","51.0","45.0","oil paint","USA"
"Leonardo da Vinci","Portrait of Isabella d'Este","1499","63.0","46.0","chalk","France"
"Leonardo da Vinci","The Last Supper","1495","460.0","880.0","tempera","Italy"
我需要阅读像上面的一个文件,并将其转换成看起来像这样一本字典:
sample_dict = {
"Pablo Picasso": [("Guernica", 1937, 349.0, 776.0, "oil paint", "Spain")],
"Leonardo da Vinci": [("Mona Lisa", 1503, 76.8, 53.0, "oil paint", "France"),
("Portrait of Isabella d'Este", 1499, 63.0, 46.0, "chalk", "France"),
("The Last Supper", 1495, 460.0, 880.0, "tempera", "Italy")],
"Vincent van Gogh": [("Cafe Terrace at Night", 1888, 81.0, 65.5, "oil paint", "Netherlands"),
("Self-Portrait with Bandaged Ear",1889, 51.0, 45.0, "oil paint", "USA")]
}
这里就是我有这么远。我目前的代码工作,但不会像上面的例子那样将文件转换成字典。感谢您的帮助
def convertLines(lines):
head = lines[0]
del lines[0]
infoDict = {}
for line in lines:
infoDict[line.split(",")[0]] = [tuple(line.split(",")[1:])]
return infoDict
def read_file(filename):
thefile = open(filename, "r")
lines = []
for i in thefile:
lines.append(i)
thefile.close()
mydict = convertLines(read_file(filename))
return lines
只想给我的代码一对夫妇的小变化返回正确的结果,或者我需要以不同的方式处理这个?它确实显示我的当前代码读取完整文件。感谢您的任何帮助
编辑:@Julien它正在工作(但不正确),直到我今天早上做了一些修改它现在给出了递归错误。
你会得到什么结果,你不满意?这是相关的信息,所以请分享! – Julien
我敢打赌,你只是覆盖每个艺术家的结果,而不是追加到列表中...... – Julien
我当前的代码实际上是递归错误,但基本上我还没有能够创建一个字典,其中的关键是艺术家名字和价值观是他们的绘画,所以对于上面的例子应该有3个键,即使名字可以在文件中重复并且值是他们的绘画(1为毕加索,3为达芬奇,2为梵高) – n00bprogrammer22