使用 pyparsing 匹配行开头的空格
我正在尝试使用 pyparsing 解析统一的差异文件作为练习,但我无法得到正确的结果。这是我的 diff 文件中给我带来麻烦的部分:
(... some stuff over...)
banana
+apple
orange
第一行以“”开头,然后是“banana”。我有以下用于解析行的表达式:
linestart = Literal(" ") | Literal("+") | Literal("-")
line = linestart.leaveWhitespace() + restOfLine
这在解析单行时有效,但是当我尝试解析整个文件时,“leaveWhitespace”指令使解析器从最后一行的末尾开始。在我的示例中,解析“香蕉”后,下一个字符是“\n”(因为leaveWhitespace),并且解析器尝试匹配“”或“+”或“-”,因此抛出错误。
我该如何正确处理这个问题?
I'm trying to parse a unified diff file using pyparsing as an exercise and I can't get something right. Here the part of my diff file that's causing me troubles :
(... some stuff over...)
banana
+apple
orange
The first line starts with " " then "banana". I have the following expression for parsing a line :
linestart = Literal(" ") | Literal("+") | Literal("-")
line = linestart.leaveWhitespace() + restOfLine
This works when parsing a single line, but when I try to parse the whole file, the "leaveWhitespace" instruction make the parser start at the end of the last line. In my example, after parsing " banana", the next char is "\n" (because of leaveWhitespace) and the parser tries to match " " or "+" or "-" and so throws an error.
How can I handle this correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以一次读取并解析一行。以下代码对我有用。
输出是
或者如果你必须解析几行,你可以显式指定 LineEnd
You can read and parse one line at a time. The following code works for me.
And the output is
Or if you have to parse several lines, you can explicitly specify the LineEnd