繁体   English   中英

如何简化我的工作 Python3 正则表达式并使其更加 Pythonic

[英]How to simplify my working Python3 regex and make it more Pythonic

我创建了一段工作(长)代码来检查目录中是否存在文件,如果存在,则提取两个字符串值并将它们转换为浮点数。

try:
    # use the file name from earlier and append .HDR
    filename = sel_file +'.HDR'

    # open the text file if found, read in the file
    f = open(filename, 'r+')
    data = f.read()
    f.close()

    # these regex are to look for the 2 strings and extracts the values in quotation marks after them
    # the value in quotes is a number written as string. There is only 1 CTRW & several PTR
    c_pat = r'CTRW,\"(.*?)\"'
    p_pat = r'\nPTR,\"(.*?)\"'

    # implements the regex on the file
    c = re.findall(c_pat, data)
    p = re.findall(p_pat, data)

    # for some reason the c & p are saved as lists so this converts to a string and then a float
    ctrw_str = int(float( ''.join(map(str,c))))
    ptr_str = int(float(''.join(map(str,p))))

    # saves to the necessary variable for use later in the code
    CTRW = ctrw_str
    PTR = ptr_str

except IOError:
    # If the file is not in the directory then the following values are used
    CTRW = 120
    PTR = 197
    pass

该文件作为字符串读入,但由于某些未知原因,正则表达式捕获并创建一个列表,这不是什么大问题,但需要转换为字符串然后转换为浮点数的额外步骤。

对 PTR 值的搜索会在源文件中返回多个值,但我想要的正是 PTR,这就是为什么我将\\n用于p_pat正则表达式搜索而不是在c_pat正则表达式中。

我想看看这里是否有人对如何将其缩小为更少的行并使其更加 Pythonic 有一个好主意。

难怪你得到列表, re.findall返回一个找到的字符串列表。

如果 - 正如您所说 - 您想“提取两个字符串值并将它们转换为浮点数”,请使用re.search获取第一个匹配项:

try:
    filename = sel_file +'.HDR'
    with open(filename, 'r+') as f:
        data = f.read()
        c = re.search(r'CTRW,"([^"]*)"', data)
        if c:
            CTRW = int(float(c.group(1)))
        p = re.search(r'\nPTR,"([^"]*)"', data)
        if p:
            PTR = int(float(p.group(1)))
except IOError: # If the file is not in the directory then the following values are used
    CTRW = 120
    PTR = 197
    pass

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM