學習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