【问题标题】:Converting text file into dictionary python将文本文件转换为字典python
【发布时间】:2020-12-16 00:06:23
【问题描述】:

我有一个类似这样的文本文件:

banana
delicious
yellow

watermelon
big
red

orange
juicy
vitamin c

我正在尝试将此文本文件转换为字典(水果名称作为键,它的几行描述作为各种值)。以下是我当前的代码。

f = open("filepath", 'w')
myplant = {}
for line in f:
    k, v = line.strip().split('\n\n')
    myplant[k.strip()] = v.strip()
f.close()

但我收到以下错误:

ValueError: not enough values to unpack (expected 2, got 1)

谁能帮我调试我的问题。谢谢!

【问题讨论】:

    标签: python python-3.x dictionary text-files valueerror


    【解决方案1】:
    • 最短的解决方案(在编辑问题之前):

    myplant = dict((i[0], i[1:3]) for i in (line.strip().split('\\n') for line in f if line != '\n') if i)
    print(myplant)
    

    输出:

    {'banana ': [' delicious ', ' yellow '], 
     'watermelon ': [' big ', ' red '], 
     'orange ': [' juicy ', ' vitamin c ']}
    
    • 通用解决方案:

    我们可以获取到换行符\n 之前的所有值,并将其存储在一个临时列表中。然后将其存储到数据列表中。

    temporary = []
    
    for line in f:
        if line.strip().split('\\n')[0]:
            if len(line.strip().split('\\n')) > 2:
                temporary = list(i.strip() for i in line.strip().split('\\n') if i.strip())
            else:
                temporary.append(line.strip().split('\\n')[0])
        elif temporary:
            data.append(temporary)
            temporary = []
    

    结果将是:

    [['banana', 'delicious', 'yellow'], 
     ['watermelon', 'big', 'red'], 
     ['orange', 'juicy', 'vitamin c']]
    

    现在对于每个列表,第一项将是键,其余的是值。

    myplant = dict((val[0], val[1:]) for val in data)
    

    输出:

    {'banana': ['delicious', 'yellow'], 
     'watermelon': ['big', 'red'], 
     'orange': ['juicy', 'vitamin c']}
    
    • 详细解释:

    从文本文件中读取时应使用r


    f = open("filepath", 'r')
    

    返回字典项时使用键、值元组。

    k = line.strip().split('\\n')  
    

    您可以使用k.remove("") 删除空字符串。

    • 漫长的路:

      while "" in k:
          k.remove("")
      
    • 捷径:

      k = [i for i in k if i]
      
    • 输出:


    {'banana ': [' delicious ', ' yellow '], 
     'watermelon ': [' big ', ' red '], 
     'orange ': [' juicy ', ' vitamin c ']}
    
    • 代码:

    f = open("filepath", 'r')
    myplant = {}
    for line in f:
        k = line.strip().split('\\n')
        # while "" in k:
        #     k.remove("")
        k = [i for i in k if i]
        if k:
            myplant[k[0]] = k[1:]
    print(myplant)
    f.close()
    

    【讨论】:

      【解决方案2】:

      当您遍历f 时,您遍历了由'\n' 分隔的行,因此在一行中永远不会有'\n\n',只有一个,所以@987654324 @ 永远不会有两个值,因为在 line 中没有出现 '\n\n'。这就是你的错误的根源。

      以下是解决这个问题的“可爱”方法。不过,我鼓励您自己想出另一种方法。

      In [1]: !cat filepath.txt
      banana
      delicious
      yellow
      
      watermelon
      big
      red
      
      orange
      juicy
      vitamin c
      
      
      In [2]: import itertools
      
      In [3]: result = {}
         ...: with open('filepath.txt') as f:
         ...:     for empty_line, group in itertools.groupby(f, lambda x: x == '\n'):
         ...:         if empty_line:
         ...:             continue
         ...:         fruit, *desc = map(str.strip, group)
         ...:         result[fruit] = desc
         ...:
      
      In [4]: result
      Out[4]:
      {'banana': ['delicious', 'yellow'],
       'watermelon': ['big', 'red'],
       'orange': ['juicy', 'vitamin c']}
      

      【讨论】:

        【解决方案3】:

        好吧,您以“w”打开文件,这是相同的写入模式。这导致了for循环中的问题。执行只读操作时应使用“r”。

        【讨论】:

          猜你喜欢
          • 2023-03-15
          • 2023-03-20
          • 2020-09-24
          • 1970-01-01
          • 2017-11-08
          • 1970-01-01
          相关资源
          最近更新 更多