我有这个文本文件
application/andrew-inset ez
application/applixware aw
application/atom+xml atom
application/atomcat+xml atomcat
application/atomsvc+xml atomsvc
application/ccxml+xml ccxml
application/cdmi-capability cdmia
application/cdmi-container cdmic
image/jpeg jpeg jpg jpe
我希望转换为密钥值对的python dictonary。 我怎么能这样做。
在多个值的情况下我也很困惑。我应该怎么做。
我想从mimetypes
获取文件扩展名所以基本上在多重值的情况下我想得到第一个。
E,G
mydict['image/jpeg']
应该返回jpeg
这就是我所想的
import shlex
f = open("mimetypes.txt","r")
mydict = dict()
for line in f:
k,v = shlex.split(line.strip())
mydict[k.strip()] = v.strip()
f.close()
f2 = open("mimetest.txt","w")
f2.write(mydict)
f2.close()
我得到了这个
Traceback (most recent call last):
File "makedict.py", line 5, in <module>
k,v = shlex.split(line.strip())
ValueError: too many values to unpack
答案 0 :(得分:3)
修改:根据您的更新,您非常接近 - 问题在于这一行:
k,v = shlex.split(line.strip())
如您所知,对于包含两个项目的任何元素,它都可以正常工作,但是当您有多个项目时会出现问题。例如:
In [1]: import shlex
In [2]: line = 'one two'
In [3]: k,v = shlex.split(line.strip())
In [4]: print k, v
one two
In [5]: line = 'one two three'
In [6]: k,v = shlex.split(line.strip())
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/<ipython console> in <module>()
ValueError: too many values to unpack
发生的事情是你试图使用三个项目的列表分配两个变量,这将给你这个错误。您可以在代码中做的一件事是通过执行以下操作来限制返回的列表以仅返回两个项目:
In [7]: line = 'one two three'
In [8]: k,v = shlex.split(line.strip())[:2]
In [9]: print k, v
one two
一般的想法是你创建一个字典,打开文件,然后在每一行,剥离尾部换行符,拆分空格并获取结果列表的前两个元素:
In [5]: d = {}
In [6]: with open('mime.txt', 'rb') as f:
...: for line in f:
...: mime, val = line.strip().split()[:2]
...: d[mime] = val
...:
...:
In [7]: d
Out[7]:
{'application/andrew-inset': 'ez',
'application/applixware': 'aw',
'application/atom+xml': 'atom',
'application/atomcat+xml': 'atomcat',
'application/atomsvc+xml': 'atomsvc',
'application/ccxml+xml': 'ccxml',
'application/cdmi-capability': 'cdmia',
'application/cdmi-container': 'cdmic',
'image/jpeg': 'jpeg'}
In [8]: d['image/jpeg']
Out[8]: 'jpeg'
如果你需要存储所有项目,你可以这样做:
In [1]: d = {}
In [2]: with open('mime.txt', 'rb') as f:
...: for line in f:
...: line = line.strip().split()
...: d[line[0]] = line[1:]
...:
...:
In [3]: d
Out[3]:
{'application/andrew-inset': ['ez'],
'application/applixware': ['aw'],
'application/atom+xml': ['atom'],
'application/atomcat+xml': ['atomcat'],
'application/atomsvc+xml': ['atomsvc'],
'application/ccxml+xml': ['ccxml'],
'application/cdmi-capability': ['cdmia'],
'application/cdmi-container': ['cdmic'],
'image/jpeg': ['jpeg', 'jpg', 'jpe']}
这包括所有MIME类型,因此如果您只想要第一个类型,则可以调用给定类型值的第一个元素:
In [4]: d['image/jpeg'][0]
Out[4]: 'jpeg
答案 1 :(得分:0)
另一种方式是:
dic = {}
file = open("filename","r")
contents = file.readlines()
for content in contents:
value = filter(lambda a: a !='',content.split(" "))
dic[value[0]] = value[1]
file.close()
print dic['image/jpeg']
我们将每行拆分为“”,然后删除列表中的任何''。然后我们为字典分配值。