从python中的文件中提取部分文本

时间:2016-05-24 23:15:22

标签: python string python-2.7

我有一组文本文件,形式如下:

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

在代码段的上方和下方还有更多文字。我希望能够为每个文本文件执行以下操作:

将NSF程序和Fld Applictn编号存储在列表中,并将相关文本存储在另一个列表中

所以,在上面的例子中,对于第i个文本文件,我想要以下内容:

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

在python中有一个干净的方法吗?我更喜欢python,因为我使用os.walk来解析存储在子目录中的所有文本文件。

2 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <!-- view background color -->
    <solid
        android:color="#00000000" >
    </solid>

    <!-- view border color and width -->
    <stroke
        android:width="1dp"
        android:color="#282828" >
    </stroke>

    <!-- The radius makes the corners rounded -->
    <corners
        android:radius="5dp"   >
    </corners>

</shape>

因此,值和值1具有您要求的特定值

答案 1 :(得分:0)

您可以尝试类似

的内容
yourtextlist = yourtext.split(':')
numbers = []
for slice in yourtextlist:
    l = slice.split()
    try:
        numbers.append(int(l[0]))
    except ValueError:
        pass