我有这个文本文件
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字典。我怎样才能做到这一点。
如果有多个值,我也很困惑我应该怎么做。
我想从 mimetypes 中获取文件扩展名
所以基本上在多重价值的情况下我想得到第一个。
例如
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