通过python获取{}内文件的路径

2024-09-30 03:22:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个代码,我只想让/X/Y/Z/C,/X/Y/Z/D,/X/Y/Z/E返回(不管在-tree后面是什么)。 它实际上应该读取文件,忽略所有内容,直到看到WFS,然后在{}中获取信息,找到树并返回路径。你知道吗

我是Python的初学者。匹配模式不起作用,因为我认为路径每天都在变化。 任何帮助都将不胜感激。你知道吗

代码:

DEFAULTS
{
    FS
    {
        -A      AAA
        -B

    }  -aaaaaa
    C
    {

    } 
}

D "FW0" 
{
} 

WFS "C:" XXXX:"/C"
{
    -trees
        "/X/Y/Z/C"
        "/X/Y/Z/D"
        "/X/Y/Z/E"
    -A  AAA
}

Tags: 文件代码路径tree内容模式fstrees
3条回答

我不确定您的模式的具体变化是什么,但您可以使用正则表达式组:

import re

myjunk = open("t.txt", "r")

for line in myjunk:
    if re.match('(/[A-Z])*', line)
        print line,

您可能需要稍微摆弄一下regex,但这里的重点是投入一点时间学习regex,您不必处理其他地方建议的一些不必要的复杂解决方案。Regex是一种小型语言,专门为许多与文本相关的东西而构建,它是非常重要的知识,甚至对于python新手来说也是如此。你会很高兴把时间花进去的!python社区很有帮助,所以为什么不加入IRC,我们将在您最喜欢的python频道中看到您的实时帮助。你知道吗

祝你好运,如果你需要更多帮助,请告诉我。你知道吗

睡衣

我对你的文件的布局有点困惑,但是有什么理由不逐行解析它吗?你知道吗

def parse():
    with open('data.txt') as fptr:
        for line in fptr:
            if line.startswith('WFS'):
                for line in fptr:
                    if line.strip().startswith('-trees'):
                        result = []
                        for line in fptr:
                            if line.strip().startswith('"'):
                                result.append(line.strip())
                            else:
                                return result

这不太好,但我想会有用的!让我们试试:

In [1]: !cat temp.txt
DEFAULTS
{
    FS
    {
        -A      AAA
        -B

    }  -aaaaaa
    C
    {

    } 
}

D "FW0" 
{
} 

WFS "C:" XXXX:"/C"
{
    -trees
        "/X/Y/Z/C"
        "/X/Y/Z/D"
        "/X/Y/Z/E"
    -A  AAA
}

In [2]: %cpaste
Pasting code; enter ' ' alone on the line to stop or use Ctrl-D.
:def parse():
:    with open('temp.txt') as fptr:
:        for line in fptr:
:            if line.startswith('WFS'):
:                for line in fptr:
:                    if line.strip().startswith('-trees'):
:                        result = []
:                        for line in fptr:
:                            if line.strip().startswith('"'):
:                                result.append(line.strip())
:                            else:
:                                return result
:
: 

In [3]: parse()
Out[3]: ['"/X/Y/Z/C"', '"/X/Y/Z/D"', '"/X/Y/Z/E"']

基于状态机的词法分析器可以可靠地完成这一任务。你知道吗

它可以识别我们感兴趣的文件结构:嵌套的花括号、命名的节(一个标识符和一个左大括号在下面的一行;这个只关心顶级节)和子句(在顶级节中由-identifier开始,可能后跟数据行,并由另一个子句或节的结尾终止)。你知道吗

然后它继续读取文件并打印找到的数据行(如果它们恰好位于我们感兴趣的节和子句中)。它还会在找到它们时设置一个标志,以便在该子句结束后立即退出。你知道吗

f = open("t.txt")

import re

identifier=None
brace_level=0
section=None
clause=None
req_clause_found=False
def in_req_clause(): return section=='WFS' and clause=='trees'

for l in (l.strip() for l in f):
    if req_clause_found and not in_req_clause(): break

    m=re.match(r'[A-Z]+',l)    #adjust if section names can be different
    if m and section is None:
        identifier=m.group(0)
        continue
    m=re.match(r'\{(\s|$)',l)
    if m:
        brace_level+=1
        if identifier is not None and brace_level==1:
            section=identifier
            identifier=None
        continue
    else: identifier=None
    m=re.match(r'\}(\s|$)',l)
    if m:
        brace_level-=1
        if brace_level==0: section=None
        clause=None
        continue
    m=re.match(r'-([A-Za-z]+)',l)   #adjust if clause names can be different
    if m and brace_level==1:
        clause=m.group(1)
        continue
    m=re.match(r'"(.*)"$',l)
    if m and in_req_clause():
        print m.group(1)
        req_clause_found=True
        continue

在样本上,这个输出 你知道吗

/X/Y/Z/C
/X/Y/Z/D
/X/Y/Z/E

相关问题 更多 >

    热门问题