Pyparsing:空格作为有效令牌
我正在使用 pyparser 处理十六进制到文本转换器的输出。它每行打印 16 个字符,用空格分隔。如果十六进制值是 ASCII 可打印字符,则打印该字符,否则转换器输出一个句点 (.)
大多数输出如下所示:
. a . v a l i d . s t r i n g .
. a n o t h e r . s t r i n g .
. e t c . . . . . . . . . . . .
我描述这一行的 pyparsing 代码是:
dump_line = 16 * Word(printables, exact=1)
这工作正常,直到十六进制到-text 转换器命中十六进制值 0x20,这会导致它输出空格。
l i n e . w . a . s p a c e .
在这种情况下,pyparsing 会忽略输出的空格并占用下一行中的字符以形成 16 个字符的“配额”。
有人可以建议我如何告诉 pyparsing 期望 16 个字符,每个字符用空格分隔,其中空格也可以是有效字符?
提前致谢。 J
I'm using pyparser to process the output of a hex-to-text converter. It prints out 16 characters per line, separated by spaces. If the hex value is an ASCII-printable character, that character is printed, otherwise the converter outputs a period (.)
Mostly the output looks like this:
. a . v a l i d . s t r i n g .
. a n o t h e r . s t r i n g .
. e t c . . . . . . . . . . . .
My pyparsing code to describe this line is:
dump_line = 16 * Word(printables, exact=1)
This works fine, until the hex-to-text converter hits a hex value of 0x20, which causes it to output a space.
l i n e . w . a . s p a c e .
In that case, pyparsing ignores the outputted space and takes up characters from the following line to make the "quota" of 16 characters.
Can someone please suggest how I can tell pyparsing to expect 16 characters, each separated by a space, where a space can also be a valid character?
Thanks in advance.
J
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于它有大量的空格,因此您需要告诉您的字符表达式不保留前导空格。请参阅下面的 dumpchar 定义,了解这是如何完成的:
打印:
Since this has significant whitespace, you'll need to tell your character expression to leave leading whitespace alone. See how this is done below in the definition of dumpchar:
Prints:
考虑使用另一种方法来删除空格
Consider using another way to remove the spaces