python - 从 PyParsing 中的多行引用字符串中删除 \n
问题描述
我正在解析带有以下内容的多行引号字符串:
带有字符串的文件(test.txt):
PROPERTY PName "Multiline quoted
string" ;
Python代码:
linebreak = pp.Suppress(';')
identifier = pp.Word(pp.alphanums + '._!<>/[]$')
qs = pp.QuotedString('"', multiline = True)
ifile = open("test.txt",'r')
test_string = ifile.read()
ifile.close()
PROPERTY = (pp.Suppress(pp.Keyword('PROPERTY'))
+ identifier('propName')
+ qs('propValue')
+ linebreak
)
for t, s, e in PROPERTY.scanString(test_string):
t.asDict()
产生:
"PROPERTY": {
"propName": "PName",
"propValue": "Multiline quoted \n string"
}
是否可以在解析期间删除 '\n' ?
解决方案
原来我找到了解决方案。它可以作为示例,因为用户指南中没有。
只需要escChar='\n'
在 qs 中插入:
qs = pp.QuotedString('"', multiline = True, escChar='\n')
哪个产生:
"PROPERTY": {
"propName": "PName",
"propValue": "Multiline quoted string"
}
推荐阅读
- java - 字符串数组不反向输出
- c++ - FwpmFilterAdd0 失败 (-2144206813)
- java - Java 按制造商、型号和年份升序查找最旧的汽车型号?
- python - 如何在这个 PyGame 代码中找到逻辑错误?
- node.js - Loopback 4:当我将环境变量放入常量并导入时,环境变量未定义
- java - 从 Jenkins 工作,得到 java.io.FileNotFoundException: 我能够在 eclipse 中运行同一个项目
- python - Tkinter-Text:让文本框将多行识别为单独的输入?
- angular - 带参数和获取参数的角路由器链接
- ios - 我应该使用哪种材料在 iOS 中创建底页
- java - 如何声明子类别java