擅长:python、mysql、java
<p>以下是一种方法:</p>
<pre><code>#! /usr/bin/env python3
keystr="ml"
k=''
d = {}
with open ("testp.txt") as file2dict:
for line in file2dict:
li = line.strip()
# This uses your keystr, but you could say if not line.startswith("/"):
if keystr in li:
k = li
elif li and k:
d[k] = d.get(k,[]) + [li]
print(d)
</code></pre>