【问题标题】:Function to read from a file and return as a dictionary?从文件中读取并作为字典返回的函数?
【发布时间】:2016-11-13 19:12:47
【问题描述】:

学习 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 文件
  • 发生了什么事? - 关于这个特定问题有moremore 问题...
  • 您在为哪一部分苦苦挣扎?看起来您甚至没有尝试创建字典或返回任何内容。

标签: python python-3.x dictionary


【解决方案1】:

一种方法是使用csv 模块和setdefault 方法dicts:

>>> 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')]})
>>> 

找出获得所需数据类型的最佳方法留作练习......显然这一切都是从一开始就完成的。

【讨论】:

    【解决方案2】:

    您的输入文件是 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 等输出

    【讨论】:

      【解决方案3】:

      使用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')]}
      

      【讨论】:

        猜你喜欢
        • 2017-03-26
        • 2017-05-19
        • 1970-01-01
        • 1970-01-01
        • 2013-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多