Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何提取和解析字符串文本文件中的字符串字段_Python - Fatal编程技术网

Python 如何提取和解析字符串文本文件中的字符串字段

Python 如何提取和解析字符串文本文件中的字符串字段,python,Python,我试图从下面的字符串文本示例行中解析出几个数字和名称。 该文件与下面的格式类似 1. 00054 **/ 063076600 NAME** Days Off: 21 Cr:021:00 2. CAPS ALLL +++ VSVS 3. more lines of text 4. 00054 / 063076600 NAME Days Off: 21 Cr:074:30 5. CAPS ALLL +++ VSVS 6. more lines of text.... 我需要解析出“/”后面的数

我试图从下面的字符串文本示例行中解析出几个数字和名称。
该文件与下面的格式类似

1. 00054 **/ 063076600 NAME** Days Off: 21 Cr:021:00 
2. CAPS ALLL +++ VSVS
3. more lines of text
4. 00054 / 063076600 NAME Days Off: 21 Cr:074:30 
5. CAPS ALLL +++ VSVS
6. more lines of text....
我需要解析出“/”后面的数字(063076600)和名称(name)。 包含这些字段的行之前始终包含“/”。此外,名称在所有大写字母中。 我尝试使用str.isupper()作为name字段,但很多我不需要的文本都是大写的,比如第2行,所以这行不通

是否有某种方法可以指定如何获取“/”后面的2项并将其添加到列表中

fname =raw_input('Enter the filename:')
listOfnames = []
try:
   fhand = open(fname)
except:
   print 'File cannot be opened',fname
   exit()
count = 0
with open(fname) as f:
   for line in f:
       # break line to words
       for word in line.strip().split():
           if word.startswith('/'):
            #get the number after "/" and append
            #get the NAME and append
               count = count + 1
               listOfnames.append(word)
               try:
                   print "number is", number
                   print "name is" , name
               except:
                   print "not available"
print listOfnames
print 'count is',count
结果:

count:2
names:
    NAME: 063076600
    NAME: 063076600

mitghi,非常感谢你帮我解决了一个我一直在努力解决的问题
count:2
names:
    NAME: 063076600
    NAME: 063076600