Pyparsing:空格作为有效令牌

发布于 2024-10-10 12:10:15 字数 595 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抽个烟儿 2024-10-17 12:10:15

由于它有大量的空格,因此您需要告诉您的字符表达式不保留前导空格。请参阅下面的 dumpchar 定义,了解这是如何完成的:

hexdump = """\
. 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 . . . . . . . . . . . . 
l i n e . w . a .   s p a c e . 
. e t c . . . . . . . . . . . . 
"""

from pyparsing import oneOf, printables, delimitedList, White, LineEnd

# expression for a single char or space
dumpchar = oneOf(list(printables)+[' ']).leaveWhitespace()

# convert '.'s to something else, if you like; in this example, '_'
dumpchar.setParseAction(lambda t:'_' if t[0]=='.' else None)

# expression for a whole line of dump chars - intervening spaces will
# be discarded by delimitedList
dumpline = delimitedList(dumpchar, delim=White(' ',exact=1)) + LineEnd().suppress()

# if you want the intervening spaces, use this form instead
#dumpline = delimitedList(dumpchar, delim=White(' ',exact=1), combine=True) + LineEnd().suppress()

# read dumped lines from hexdump
for t in dumpline.searchString(hexdump):
    print ''.join(t)

打印:

_a_valid_string_
_another_string_
_etc____________
line_w_a_ space_
_etc____________

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:

hexdump = """\
. 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 . . . . . . . . . . . . 
l i n e . w . a .   s p a c e . 
. e t c . . . . . . . . . . . . 
"""

from pyparsing import oneOf, printables, delimitedList, White, LineEnd

# expression for a single char or space
dumpchar = oneOf(list(printables)+[' ']).leaveWhitespace()

# convert '.'s to something else, if you like; in this example, '_'
dumpchar.setParseAction(lambda t:'_' if t[0]=='.' else None)

# expression for a whole line of dump chars - intervening spaces will
# be discarded by delimitedList
dumpline = delimitedList(dumpchar, delim=White(' ',exact=1)) + LineEnd().suppress()

# if you want the intervening spaces, use this form instead
#dumpline = delimitedList(dumpchar, delim=White(' ',exact=1), combine=True) + LineEnd().suppress()

# read dumped lines from hexdump
for t in dumpline.searchString(hexdump):
    print ''.join(t)

Prints:

_a_valid_string_
_another_string_
_etc____________
line_w_a_ space_
_etc____________
狂之美人 2024-10-17 12:10:15

考虑使用另一种方法来删除空格

>>> s=". a . v a l i d . s t r i n g ."
>>> s=s[::2]
>>> s
'.a.valid.string.'

Consider using another way to remove the spaces

>>> s=". a . v a l i d . s t r i n g ."
>>> s=s[::2]
>>> s
'.a.valid.string.'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文