NSR NSR - 2 years ago 70
Python Question

extracting part of text from file in python

I have a collection of text files that are of the form:

Sponsor : U of NC Charlotte
U N C C Station
Charlotte, NC 28223 704/597-2000

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

there's more text above and below the snippet. I want to be able to do the following, for each text file:

store the NSF program, and Fld Applictn numbers in a list, and store the associated text in another list

so, in the above example I want the following, for the i-th text file:

y_num[i] = 1468, 0308000, 56
y_txt[i] = MANUFACTURING MACHINES & EQUIP, Industrial Technology, Engineering-Mechanical

Is there a clean way to do this in python? I prefer python since I am using os.walk to parse all the text files stored in subdirectories.

Answer Source
file = open( "file","r")
for line in file.readlines():
    if "NSF" in line:
        values= line.split(":")
    elif "Fld" in line:
        values1 = line.split(":")

So values and values1 has the specific values which you are intetested

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download