【问题标题】:Reading a file into a dictionary python将文件读入字典python
【发布时间】:2016-12-03 18:39:09
【问题描述】:

我有一个文件:

Fruits:
I love apples
I also love bananas
who even likes pears?
<<<END
Bananas are yellow 
apples are not yellow..
<<<END
Vegetables:
Anything green is gross
I don’t like vegetables
<<<END
Peas are disgusting
Who even likes peas
Is potato a vegetable?
<<<END

我想取以“:”结尾的行,例如“Fruits”和“Vegetables”,并将它们作为字典的键。并将键下的每一行都设为元组列表。

到目前为止我有:

def read_file(file):
    dic = {}
    lst = []
    with open(file,'r') as file:
        for line in file:
            if line.strip("\n") == "<<<END":
                continue
            elif line.endswith(":\n"):
                a = line.strip(":\n")
                dic[a] = []
            else:
                key = line.strip(":\n")
                dic[a].append(key)
        return dic

我希望程序返回:

{'Fruits': [("I love apples", "I also love bananas", "who even like  pears"),("Bananas are yellow", "apples are not yellow..")], 'Vegetables':[("Anything green is gross", "I don't like vegetables"),("Peas are disgusting", "Who even likes peas", "Is potato a vegetable?")]}

返回:

{'Fruits': ["I love apples", "I also love bananas", "who even like  pears","Bananas are yellow", "apples are not yellow.."], Vegetables: ["Anything green is gross", "I don't like vegetables","Peas are disgusting", "Who even likes peas", "Is potato a vegetable?"]}

【问题讨论】:

  • 可能是因为您使用的是"&lt;&lt;&lt;EOT",而不是文件中指定的"&lt;&lt;&lt;END"
  • 它返回 {'fruits': [“我爱苹果”、“我也爱香蕉”、“甚至喜欢梨”、“香蕉是黄色的”、“苹果不是黄色的..” ],蔬菜:[“任何绿色的东西都很恶心”、“我不喜欢蔬菜”、“豌豆很恶心”、“谁甚至喜欢豌豆”、“土豆是蔬菜吗?”]}
  • 对不起,当我尝试发布它时它正在切断输出,所以我将它添加为上面的评论^

标签: python list dictionary tuples


【解决方案1】:

以下是一个解决方案,可让您的代码几乎完好无损。这个想法是制作一个列表,直到你得到一个 &lt;&lt;&lt;END 并将元组(由列表制成)附加到结果列表中。

def read_file(file):
    dic = {}
    lst = []
    with open(file,'r') as file:
        temp= []
        key = None
        for line in file:
            line = line.strip()
            if line == "<<<END":
                dic[key].append(tuple([key] + temp))
                temp = []
                continue
            elif line.endswith(":"):
                key = line.strip(":")
                dic[key] = []
            else:
                temp.append(line)
        return dic


print(read_file('test2.txt'))

输出:

{'Vegetables': [('Vegetables', 'Anything green is gross', 'I don\xe2\x80\x99t like vegetables'), ('Vegetables', 'Peas are disgusting', 'Who even likes peas', 'Is potato a vegetable?')], 'Fruits': [('Fruits', 'I love apples', 'I also love bananas', 'who even likes pears?'), ('Fruits', 'Bananas are yellow', 'apples are not yellow..')]}

【讨论】:

  • @Johnny,很高兴知道它有帮助。如果你觉得这个特定的答案满足你的问题,你可以考虑接受这个答案。看看如何接受来自here
  • 我如何让它返回:{'Vegetables': [('vegetables','任何绿色都是恶心的', '我不喜欢蔬菜'), ('vegetables ','豌豆很恶心','谁甚至喜欢豌豆','土豆是蔬菜吗?')],'水果':[('水果','我喜欢苹果','我也喜欢香蕉','谁甚至喜欢梨?'), ('水果','香蕉是黄色的', '苹果不是黄色的..')]}
  • 你说的让它返回是什么意思?
  • 我是怎么做到的,所以元组中的第一个元素是键的名称,正如我在上面发布的那样^
【解决方案2】:

我认为您基本上是正确的,只是缺少一些步骤来将不同的数据组分开。试试:

def read_file(path):
   d = {}
   with open(path, 'r') as f:
       lines = f.read().splitlines()
       group = None
       parts = []
       for line in lines:
           if line.endswith(":"):
               group = line[:-1]
               d[group] = []
           else:
               if line == "<<<END":
                   #  tuple-ize existing data
                   d[group].append(tuple(parts))
                   parts = []

               else:
                   # add to existing data
                   k = line[:-1]
                   parts.append(line)
       return d

对我来说返回:

>>> read_file('/tmp/1')
{'Vegetables': [('Anything green is gross', 'I don\xe2\x80\x99t like vegetables'), ('Peas are disgusting', 'Who even likes peas', 'Is potato a vegetable?')], 'Fruits': [('I love apples', 'I also love bananas', 'who even likes pears?'), ('Bananas are yellow ', 'apples are not yellow..')]}
>>>

我刚刚通过 tuple() 添加了数据的显式转换,并添加了一个临时变量“parts”来跟踪最终字典之外的数据。

【讨论】:

    【解决方案3】:

    你可以试试:

    def read_file(file):
        dic = {}
        lst = []
        same_dict = False 
        with open(file,'r') as file:
            for line in file:
                if line.endswith(":\n"):
                    a = line.strip(":\n")
                    dic[a] = []
                else:
                    if line.strip("\n") == "<<<END":
                        dic[a].append(tuple(lst))
                        lst = []
                    else:
                        key = line.strip("\n")
                        lst.append(key)
       return dic
    

    参考您的新要求,

    def read_file(file):
        dic = {}
        lst = []
        same_dict = False 
        with open(file,'r') as file:
            for line in file:
                if line.endswith(":\n"):
                    a = line.strip(":\n")
                    dic[a] = []
                else:
                    if line.strip("\n") == "<<<END":
                        dic[a].append(tuple([a]+lst))
                        lst = []
                    else:
                        key = line.strip("\n")
                        lst.append(key)
        return dic
    
    print(read_file('temp.txt'))
    

    希望对你有帮助

    【讨论】:

    • 我如何让它返回:{'Vegetables': [('vegetables','任何绿色都是恶心的', '我不喜欢蔬菜'), ('vegetables ','豌豆很恶心','谁甚至喜欢豌豆','土豆是蔬菜吗?')],'水果':[('水果','我喜欢苹果','我也喜欢香蕉','谁甚至喜欢梨?'), ('Fruits','Bananas are yellow', 'apples are not yellow..')]} 我如何让它返回,以便每个元组中的第一个元素是关键
    • 我不明白它的用途,但我已经编辑了答案,如果它符合您的要求,请标记为正确。
    猜你喜欢
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    相关资源
    最近更新 更多