【发布时间】:2016-05-24 23:15:21
【问题描述】:
我有一组文本文件,格式如下:
Sponsor : U of NC Charlotte
U N C C Station
Charlotte, NC 28223 704/597-2000
NSF Program : 1468 MANUFACTURING MACHINES & EQUIP
Fld Applictn: 0308000 Industrial Technology
56 Engineering-Mechanical
Program Ref : 9146,MANU,
Abstract :
9500390 Patterson This award supports a new concept in precision metrology,
the Extreme Ultraviolet Optics Measuring Machine (EUVOMM). The goals for this
system when used to measure optical surfaces are a diameter range of 250 mm
with a lateral accuracy of 3.3 nm rms, and a depth range of 7.5 mm w
sn-p 的上方和下方还有更多文字。对于每个文本文件,我希望能够执行以下操作:
将 NSF 程序和 Fld Applictn 编号存储在一个列表中,并将关联的文本存储在另一个列表中
所以,在上面的例子中,对于第 i 个文本文件,我想要以下内容:
y_num[i] = 1468, 0308000, 56
y_txt[i] = MANUFACTURING MACHINES & EQUIP, Industrial Technology, Engineering-Mechanical
在python中有没有一种干净的方法来做到这一点?我更喜欢 python,因为我使用 os.walk 来解析存储在子目录中的所有文本文件。
【问题讨论】:
-
根据格式的一致性,您可以使用正则表达式或简单的字符串操作或编写解析器来执行此操作。这一切都可以在 python 中实现。
-
是的,如果你经常做字符串操作,它们往往会相当乏味。虽然有时也没有那么糟糕。例如,请参阅我的答案。
标签: python string python-2.7