2016-11-13 46 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')]} 
相关问题