Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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_Regex - Fatal编程技术网

使用python从文本文件中提取大括号中包含的值

使用python从文本文件中提取大括号中包含的值,python,regex,Python,Regex,我在文本文件中有如下行: rest=separateCodes(PatientETLByMsg[i].split('<L> <+>')[1].split('<&>')[1]) CCC=PPByMsg[i].split('{')[1].split('}')[0] DDD=PPByMsg[i].split('}{')[1] EEE=PPByMsg[i].split('}{')[2] FFF=PPByMsg[i].split('}{')[3] GGG=P

我在文本文件中有如下行:

rest=separateCodes(PatientETLByMsg[i].split('<L>  <+>')[1].split('<&>')[1])

CCC=PPByMsg[i].split('{')[1].split('}')[0]
DDD=PPByMsg[i].split('}{')[1]
EEE=PPByMsg[i].split('}{')[2]
FFF=PPByMsg[i].split('}{')[3]
GGG=PPByMsg[i].split('}{')[4]
HHH=PPByMsg[i].split('}{')[5]
KKK=PPByMsg[i].split('}{')[6].split('}')[0]
0044xx aaa,bbb 01/01/0017:53{3.01}{00001}{xxx yyy DIFF}{(4.0-10.5)}{7.2}

等等

我试图提取如下值:

AAA is 0044xx aaa, bbb 

BBB is 01/01/0017:53

CCC is 3.01

DDD is 00001

EEE is xxx yyy

FFF is (4.0-10.5)

HHH is 7.2
我无法从CCC到HHH中提取包含在大括号中的值

我的剧本是这样的:

import sys

import re

import csv

def separateCodes(code):
    values = re.compile('.*?\{(.*?)\}.*?')
    field=values.findall(code)    

    for i in range(len(field)):
        print field[i]
    print"-------------------------"        

def handleError(self, record):
    raise    
with open('/xxx.TXT') as ABCfp:    
    PP=ABCfp.read()

PPwithNOrn=PP.replace('*\r','').replace('\n', '')
PPByMsg=PPwithNOrn.split('<~>')
print len(PPByMsg)

for i in range(len(PPByMsg)):

    AAA=""
    BBB=""
    CCC=""
    DDD=""
    EEE=""
    FFF=""
    GGG=""
    HHH=""

    print i,"=>",PPByMsg[i]
    if PPByMsg[i].find("<L>")!=-1:
        print "-----------------------"
        # AAA found
        AAA=PPByMsg[i].split('<L>  <+>')[0]
    # BBB found
    BBB=PPByMsg[i].split('<L>  <+>')[1].split('<&>')[0]
        # REST Found
    rest=separateCodes(PPByMsg[i].split('<L>  <+>')[1].split('<&>')[1])
导入系统 进口稀土 导入csv def分离代码(代码): values=re.compile('.*?\{(.*?\}.*?')) 字段=值。findall(代码) 对于范围内的i(len(field)): 打印字段[i] 打印“---------------------------” def手柄错误(自身,记录): 提高 以open('/xxx.TXT')作为ABCfp: PP=ABCfp.read() PPwithNOrn=PP.replace('*\r','')。replace('\n','') PPByMsg=PPwithNOrn.split(“”) 打印长度(PPByMsg) 对于范围内的i(len(PPByMsg)): AAA=“” BBB=“” CCC=“” DDD=“” EEE=“” FFF=“” GGG=“” HHH=“” 打印i,“=>”,PPByMsg[i] 如果PPByMsg[i].find(“”)=-1: 打印“---------------------------” #AAA发现 AAA=PPByMsg[i]。拆分(“”)[0] #BBB发现 BBB=PPByMsg[i]。拆分(“”)[1]。拆分(“”)[0] #剩余的 rest=separateCodes(PPByMsg[i]。拆分(“”)[1]。拆分(“”)[1])
因为我是python的新手,所以无法继续前进。请建议一种提取这些值的方法。

改为:

a,b,c = re.split('<[+&]>', i)
bits = re.split('{(.*?)}', c)[1:-1]

可以使用单个正则表达式执行整个操作:

>>> t = '0044xx aaa, bbb <+> 01/01/0017:53 <&> { 3.01}{00001 }{xxx yyy DIFF}{(4.0-10.5)}{7.2}'
>>> re.search(r'(.*?)\s<\+>\s(.*?)\s<&>\s{(.*?)\}\{(.*?)\}\{(.*?) DIFF\}\{(.*?)\}\{(.*?)\}', t).groups()
('0044xx aaa, bbb', '01/01/0017:53', ' 3.01', '00001 ', 'xxx yyy', '(4.0-10.5)', '7.2')
或者,使用
zip
to或元组赋值,例如:

>>> results = re.search(...).groups()
>>> resultdict = zip('abcdefg', results)
>>> a, b, c, d, e, f, g = results

我已完成以下要求:

rest=separateCodes(PatientETLByMsg[i].split('<L>  <+>')[1].split('<&>')[1])

CCC=PPByMsg[i].split('{')[1].split('}')[0]
DDD=PPByMsg[i].split('}{')[1]
EEE=PPByMsg[i].split('}{')[2]
FFF=PPByMsg[i].split('}{')[3]
GGG=PPByMsg[i].split('}{')[4]
HHH=PPByMsg[i].split('}{')[5]
KKK=PPByMsg[i].split('}{')[6].split('}')[0]
rest=separateCodes(PatientETLByMsg[i]。拆分(“”)[1]。拆分(“”)[1])
CCC=PPByMsg[i]。拆分('{')[1]。拆分('}')[0]
DDD=PPByMsg[i].split('}{')[1]
EEE=PPByMsg[i].split('}{')[2]
FFF=PPByMsg[i].split('}{')[3]
GGG=PPByMsg[i].split('}{')[4]
HHH=PPByMsg[i].split('}{')[5]
KKK=PPByMsg[i]。拆分('}{')[6]。拆分('}')[0]

欢迎来到stackoverflow。请让每个人都能阅读它。
EEE
你想提取值的方式正确吗?像这样使用rest=separateCodes(PatientETLByMsg[i]。split(“”)[1]。split(“”)[1])ORDER=rest.split(“{1]),c)[1:-1]打印顺序获取错误顺序=rest.splitAttributeError:“非类型”对象在用作MRN、DATETIME、rest=re.split(“”,i)bits=re.split('{(.*}),str(rest))[1:-1]获取错误返回_compile(pattern,0)。split(string,maxslit)TypeError:预期的字符串或缓冲区这意味着您的字符串与您的模式不匹配。这对我有效(Python2.7)。如果您提供更多关于“获取错误”的信息,我可能会有所帮助。您从哪里获取错误,您运行的是什么版本的Python?错误是我发布的代码,还是您在自己的代码中使用正则表达式时得到的?我使用的是Python2.6.6。我将其作为results=re.search(rest.groups)()TypeError:search()至少接受2个参数(给定1个)仔细查看我的第一个示例,并阅读re模块文档。
re.search
接受两个参数。
rest=separateCodes(PatientETLByMsg[i].split('<L>  <+>')[1].split('<&>')[1])

CCC=PPByMsg[i].split('{')[1].split('}')[0]
DDD=PPByMsg[i].split('}{')[1]
EEE=PPByMsg[i].split('}{')[2]
FFF=PPByMsg[i].split('}{')[3]
GGG=PPByMsg[i].split('}{')[4]
HHH=PPByMsg[i].split('}{')[5]
KKK=PPByMsg[i].split('}{')[6].split('}')[0]