1

有什么方法可以使用 python 在 YAML 中存储和读取这个正则表达式:

regular: /<title [^>]*lang=("|')wo("|')>/ 

有人对此有任何想法或解决方案吗?

我有以下错误:

    % ch.encode('utf-8'), self.get_mark())
yaml.scanner.ScannerError: while scanning for the next token
found character '|' that cannot start any token
  in "test.yaml", line 10, column 49

我的代码:

def test2():
    clueAppconf = open('test.yaml')
    clueContext = yaml.load(clueAppconf) 
    print clueContext['webApp']
4

1 回答 1

2

好的,看起来问题在于您选择表示此正则表达式的标量类型。如果您与标量(yaml 字符串)结婚,则需要对它阻塞的特殊字符使用双引号标量和转义码。因此,您的 yaml 应如下所示:

regular: "/<title [^>]*lang=("\x7C')wo("\x7C')>/" 

我只是转义了它正在窒息的字符以保持一些可读性,但是您可能需要转义其他字符,具体取决于它是否会引发更多错误。此外,您可以使用 unicode 转义码。看起来像这样:

regular: "/<title [^>]*lang=("\u007C')wo("\u007C')>/"

我对我的 yaml 知识有点了解,所以我不知道如何在 yaml 中维护特殊字符及其可读性。根据我对 yaml 文档的粗略扫描,这是我能找到的最好的。

于 2012-07-05T10:32:18.530 回答