简体   繁体   English

从文件读取并作为字典返回的功能?

[英]Function to read from a file and return as a dictionary?

Learning python and having trouble understanding on how to create this function to read a file and return it as a dictionary. 学习python并在理解如何创建此函数以读取文件并将其作为字典返回时遇到麻烦。 I'm aware I need to open the file and then use the .read(), but so far I'm not sure how to sort the data. 我知道我需要打开文件,然后使用.read(),但是到目前为止,我不确定如何对数据进行排序。 Since there will be multiple "titles," I'm trying to sort upper-case letters to come before all lower-case. 由于会有多个“标题”,因此我试图将大写字母排序为所有小写字母之前。 Any advice on how to proceed? 关于如何进行的任何建议?

Code I have so far: 我到目前为止的代码:

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.

Text file example: 文本文件示例:

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" 

What I want to return file as: 我要返回的文件为:

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

One approach is to use the csv module and the setdefault method for dict s: 一种方法是使用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')]} 

The more pythonic way is to use a defaultdict : 更加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')]})
>>> 

Figuring out the best way to get the data types you need is left as an exercise... as apparently this whole thing was from the beginning. 找出最佳方法来获取所需的数据类型只是一项练习……显然,这整个过程都是从一开始就开始的。

Your input file is a CSV file (comma separated values). 您的输入文件是CSV文件(用逗号分隔的值)。 There's a module called csv for reading them. 有一个名为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 will take inputs like '"Mona Lisa"' , '1234' and return outputs like 'Mona Lisa' and 1234 ast.literal_eval将接受'"Mona Lisa"''1234''"Mona Lisa"'输入,并返回诸如'Mona Lisa'1234'Mona Lisa'输出

The solution using csv.reader object and enumerate function: 使用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)

The output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM