尝试创建一个函数来读取文件并将其添加到一个有组织的字典中,然后在不更改原始字典的情况下返回它。不知道我是否正确使用多个项目和值。功能:读取文件,然后将多个项目添加到字典
返回:
{'Leonardo da Vinci': [("Portrait of Isabella d'Este", 1499, 63.0, 46.0, 'chalk', 'France'), ('The Last Supper', 1495, 460.0, 880.0, 'tempera', 'Italy')], 'Pablo Picasso': [('Guernica', 1937, 349.0, 776.0, 'oil paint', 'Spain')]}
示例文件:
file1='''"Artist","Title","Year","Total Height","Total Width","Media","Country"
"Pablo Picasso","Guernica","1937","349.0","776.0","oil paint","Spain"
"Leonardo da Vinci","The Last Supper","1495","460.0","880.0","tempera","Italy"'''
代码我到目前为止有:
def add_work (db,artist,title,year,height,width,media,country):
db = {}
with open(filename) as f:
for line in f:
(title, year, height, width, media, country) = line.split()
db[int(artist)] = (title, year, height, width, media, country)
for i in d.keys():
if i == artist #If artist in dictionary, then add it to item.
db[i].extend
elif i == title #If it has the same title as in the database, its a duplicate so return none.
return None
add_work(d1,"Leonardo da Vinci","Portrait of Isabella d'Este", 1499, 63.0,46.0, "chalk", "France")
限制:
符号顺序:按ASCII码 排序,而不是按字母顺序排列。
没有导入/集合/模块。基本建立在函数,循环和字典方法。
“ASCII整理顺序”和“字母顺序”有什么区别?你的意思是所有大写字母都写在所有小写字母之前吗?或者这涉及到非字母字符?或两者? –
是的@RoryDaulton所有大写字母都出现在所有小写字母之前。不涉及非字母字符 –
我并没有真正明白你想要做什么,但是'db [int(artist)] = ...'应该引发'ValueError',因为艺术家的名字赢了不是一个整数 - 请尝试澄清你想要做的是什么。您是否正在读取csv-File中的数据并将艺术家的所有绘画作为列表中的元素进行关联? – Maurice