学习Python和无法理解如何创建此函数读取一个文件并返回它作为一本字典。我知道我需要打开文件,然后使用.read(),但到目前为止我不知道如何对数据进行排序。由于会有多个“标题”,我试图在所有小写之前对大写字母进行排序。有关如何继续的建议?从文件中读取并作为字典返回的函数?
代码我到目前为止有:
def read_text(textname):
d = {}
with open(textname) as f:
for line in f:
(title, year, height, width, media, country) = line.split() # I need to skip the first line in the file as well which just shows the categories.
文本文件,例如:
text0='''"Artist","Title","Year","Total Height","Total
Width","Media","Country"
"Leonardo da Vinci","Mona Lisa","1503","76.8","53.0","oil paint","France"
"Leonardo da Vinci","The Last Supper","1495","460.0","880.0","tempera","Italy"
我要回文件是什么:
{'Leonardo da Vinci': [("Mona Lisa",1503,76.8,53.0,"oil paint","France"),
('The Last Supper', 1495, 460.0, 880.0, 'tempera', 'Italy')]}
@UnholySheep这是一个CSV文件 –
这是怎么回事? - 有更多](https://stackoverflow.com/questions/40566245/function-read-a-file-then-add-multiple-items-to-dictionary)和[更多](HTTPS://计算器。 COM /问题/ 40577549 /转换-CSV文件到字典的Python)的问题,在这个特别的问题...... – Maurice
的[排序值Python字典(可能的复制http://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value) – AthenAl