[英]Make python dictionary from text file
man
cat
dog
apple
sun
friend
I have text file in above format I need to make python dictionary: {'man':'cat, dog', apple: 'sun', 'friend'} words man and apple have no spaces all the others have 2 spaces before so these two should be keys and words under them should be values. 我有上述格式的文本文件,我需要制作python字典:{'man':'cat,dog',apple:'sun','friend'}单词man和apple没有空格,所有其他人之前都有2个空格这两个应该是键,其下的单词应该是值。
input_file = "/home/yan/lem.txt" input_file =“ /home/yan/lem.txt”
class myDict(dict):
def __init__(self):
self = dict()
def add(self, key, value):
self[key] = value
lema = myDict()
with open(input_file, encoding='utf8', errors='ignore') as f:
for line in f:
if not line.startswith(" "):
lema.add(line.split(), [i for i in f if i.startswith(" ")])
print(lema)
Was trying above code, but it gives not what I am expecting. 正在尝试上面的代码,但它没有提供我所期望的。
this will get you what you need: 这将为您提供所需的东西:
d = {}
k = ''
with open('lem.txt') as f:
for ln in f.readlines():
if ln.startswith(' '):
d.setdefault(k, []).append(ln.strip())
else:
k = ln.strip()
print d
you cannot use self
as your dictionary name, in python it is used to refer your instance variable although it is not reserved keyword. 您不能将self
用作字典名称,在python中,虽然不是保留关键字,但是它用于引用您的实例变量。
the class function add()
, try use dict.setdefault(key, list()).append(value) it keeps appends new values to the existing key. 类函数add()
,请尝试使用dict.setdefault(key,list())。append(value),它会不断向现有键添加新值。
override __str__ method which display your expected string format when you invoke print(lema)
覆盖__str__方法,该方法在调用print(lema)
时显示您期望的字符串格式
$ cat test.py $ cat test.py
input_file = "lem.txt"
class myDict(dict):
def __init__(self):
self.mydict = dict()
def add(self, key, value):
self.mydict.setdefault(key, list()).append(value)
def __str__(self):
tmp = list()
for key, value in self.mydict.items():
tmp.append("{0}:{1}".format(key, value))
return "\n".join(tmp)
lema = myDict()
with open(input_file) as f:
for line in f:
if not line.strip():
'''skips blank line'''
continue
elif not line.startswith(" "):
key = line.strip()
elif line.startswith(" "):
value = line.strip()
lema.add(key,value)
print(lema)
The output is:
$python test.py
apple:['sun', 'friend']
man:['cat', 'dog']
You could slurp the file into one big string, s
, and then do something like this: 您可以将文件插入一个大字符串s
,然后执行以下操作:
s = """man
cat
dog
apple
sun
friend"""
d = {}
for line in s.replace("\n ",',').split('\n'):
words = line.split(',')
d[words[0]] = words[1:]
>>> d
{'man': ['cat', 'dog'], 'apple': ['sun', 'friend']}
To get a feel for what is happening here: 感受一下这里发生的事情:
>>> s.replace("\n ",',')
'man,cat,dog\napple,sun,friend'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.