2016-11-13 94 views
1

學習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')]} 
+0

@UnholySheep這是一個CSV文件 –

+1

這是怎麼回事? - 有更多](https://stackoverflow.com/questions/40566245/function-read-a-file-then-add-multiple-items-to-dictionary)和[更多](HTTPS://計算器。 COM /問題/ 40577549 /轉換-CSV文件到字典的Python)的問題,在這個特別的問題...... – Maurice

+0

的[排序值Python字典(可能的複製http://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value) – AthenAl

回答

0

輸入文件是一個CSV文件(逗號分隔值)。有一個名爲csv的模塊用於閱讀它們。

import csv 
import ast 
def our_function(filename): 
    output = {} 
    with open(filename) as f: 
     r = csv.reader(f) 
     _ = next(r) #ignore the first line 
     for line in r: 
      head, *tail = map(ast.literal_eval, line) #make values the right types 
      if head in output: 
       output[head].append(tuple(tail)) 
      else: 
       output[head] = [tuple(tail)] 
    return output 

ast.literal_eval將輸入像'"Mona Lisa"''1234'和返回輸出等'Mona Lisa'1234

2

的一種方法是使用csv模塊和setdefault方法dict S:

>>> import csv 
>>> with open('data.csv') as f: 
... d = {} 
... reader = csv.reader(f) 
... header = next(f) # skip first line, save it if you want to 
... for line in reader: 
...  artist, *rest = line 
...  d.setdefault(artist,[]).append(tuple(rest)) 
... 
>>> d 
{'Leonardo da Vinci': [('Mona Lisa', '1503', '76.8', '53.0', 'oil paint', 'France'), ('The Last Supper', '1495', '460.0', '880.0', 'tempera', 'Italy')]} 

的更pythonic的方式是使用defaultdict

>>> from collections import defaultdict 
>>> with open('data.csv') as f: 
... d = defaultdict(list) 
... reader = csv.reader(f) 
... header = next(f) # skip header 
... for line in reader: 
...  artist, *rest = line 
...  d[artist].append(rest) 
... 
>>> d 
defaultdict(<class 'list'>, {'Leonardo da Vinci': [('Mona Lisa', '1503', '76.8', '53.0', 'oil paint', 'France'), ('The Last Supper', '1495', '460.0', '880.0', 'tempera', 'Italy')]}) 
>>> 

搞清楚獲取所需數據類型的最佳方法是作爲一個練習......顯然這整個事情是從一開始。

0

使用csv.reader對象和enumerate功能的解決方案:

import csv 

picture_info = {} 
# let's say: `pictures.csv` is your initial file 
with open('pictures.csv', 'r', newline='\n') as fh: 
    r = csv.reader(fh) 
    for k, line in enumerate(r): 
     if k == 0: continue 
     if not picture_info.get(line[0], None): 
      picture_info[line[0]] = [tuple(line[1:])] 
     else: 
      picture_info[line[0]].append(tuple(line[1:])) 

print(picture_info) 

輸出:

{'Leonardo da Vinci': [('Mona Lisa', '1503', '76.8', '53.0', 'oil paint', 'France'), ('The Last Supper', '1495', '460.0', '880.0', 'tempera', 'Italy')]} 
相關問題