我有这样一个文本文件:
APPENDIX -- GLOSSARY
-------------------------------------------------------------------
Asymmetrical Encryption:
Encryption using a pair of keys--the first encrypts a
Big-O Notation, Complexity:
Big-O notation is a way of describing the governing.
In noting complexity orders, constants and multipliers are
conventionally omitted, leaving only the dominant factor.
Compexities one often sees are:
#*------------- Common Big-O Complexities ---------------#
O(1) constant
Birthday Paradox:
The name "birthday paradox" comes from the fact--surprising
Cyclic Redundancy Check (CRC32):
See Hash. Based on mod 2 polynomial operations, CRC32 produces a
32-bit "fingerprint" of a set of data.
Idempotent Function:
The property that applying a function to its return value
'G=lambda x:F(F(F(...F(x)...)))'.
我想解析文本文件,使其具有如下输出:
{'Asymmetrical Encryption': Encryption using a pair of keys--the first encrypts a,
'Big-O Notation, Complexity':'Big-O notation is a way of describing the governing. In noting complexity orders, constants and multipliers are conventionally omitted, leaving only the dominant factor. Compexities one often sees are: #*------------- Common Big-O Complexities ---------------# O(1) constant}', ..so on }
我就是这么做的:
dic = {}
with open('appendix.txt', 'r') as f:
data = f.read()
lines = data.split(':\n\n')
for line in lines:
res = line.split(':\n ')
field = res[0]
val = res[1:]
dic[field] = val
这会弄乱文本中的:
值,不管标题是什么。输出不正确
如果要基于第一个空格解析文本,可以使用如下脚本:
相关问题 更多 >
编程相关推荐