[英]Converting text file into dictionary python
I have a text file similar to this:我有一个类似这样的文本文件:
banana
delicious
yellow
watermelon
big
red
orange
juicy
vitamin c
And I'm trying to convert this text file as dictionary (fruit names as key and its few lines of descriptions as various values).我正在尝试将此文本文件转换为字典(水果名称作为键,它的几行描述作为各种值)。 Following is my current code.
以下是我当前的代码。
f = open("filepath", 'w')
myplant = {}
for line in f:
k, v = line.strip().split('\n\n')
myplant[k.strip()] = v.strip()
f.close()
But I got the following error:但我收到以下错误:
ValueError: not enough values to unpack (expected 2, got 1)
Can anyone help me debug my problem.谁能帮我调试我的问题。 Thank you!
谢谢!
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)
Output:输出:
{'banana ': [' delicious ', ' yellow '],
'watermelon ': [' big ', ' red '],
'orange ': [' juicy ', ' vitamin c ']}
We can get all the values until the newline \\n
and store it in a temporary list.我们可以获得所有值直到换行符
\\n
并将其存储在一个临时列表中。 Then store it into the data list.然后将其存储到数据列表中。
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 = []
The result will be:结果将是:
[['banana', 'delicious', 'yellow'],
['watermelon', 'big', 'red'],
['orange', 'juicy', 'vitamin c']]
Now for each list, the first item will be the key and the rest are values.现在对于每个列表,第一项将是键,其余是值。
myplant = dict((val[0], val[1:]) for val in data)
Output:输出:
{'banana': ['delicious', 'yellow'],
'watermelon': ['big', 'red'],
'orange': ['juicy', 'vitamin c']}
You should use r
when you are reading from the text file从文本文件中读取时应该使用
r
f = open("filepath", 'r')
The key, value tuple is used when you are returning the dictionary items.返回字典项时使用键值元组。
k = line.strip().split('\\n')
You could use k.remove("")
to remove empty strings.您可以使用
k.remove("")
删除空字符串。
Long way:很长的路要走:
while "" in k: k.remove("")
Short way:简短的方法:
k = [i for i in k if i]
Output:输出:
{'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()
When you iterate over f
, you are iterating over lines delimited by '\\n'
, so there will *never be '\\n\\n'
in a single line, only ever one, so .split('\\n\\n')
will never have two value, because there is no occurrence of '\\n\\n'
in the line
.当您迭代
f
,您正在迭代由'\\n'
分隔的行,所以在一行中*永远不会有'\\n\\n'
,只有一个,所以.split('\\n\\n')
永远不会有两个价值,因为没有出现'\\n\\n'
的line
。 That is the source of your error.这就是你错误的根源。
The following is a "cute" way to solve this.以下是解决此问题的“可爱”方法。 I encourage you to figure out another approach on your own, though.
不过,我鼓励您自己找出另一种方法。
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']}
Well, you opened the file in "w" which is write mode for the same.好吧,您以“w”打开文件,这是相同的写入模式。 Which causes the problem in the for loop.
这导致了 for 循环中的问题。 You should use 'r' when performing read-only operations.
执行只读操作时应使用“r”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.