Pyparsing: space as a valid token

I am using pyparser to handle the output of a hex converter. It prints 16 characters per line, separated by spaces. If the hexadecimal value is an ASCII print character, this character is printed; otherwise, the converter returns a period (.)

Basically the output is as follows:

. a . valid . string . . another . string . . etc . . . . . . . . . . . . 

My pyparsing code to describe this line:

 dump_line = 16 * Word(printables, exact=1) 

This works fine until the hex converter reaches the hex value 0x20, which causes it to exit.

 line . w . a . space . 

In this case, pyparsing ignores the displayed space and captures the characters from the next line to make a "quota" of 16 characters.

Can anyone suggest, how can I say that pyparsing expects 16 characters, each of which is separated by a space, where space can also be a valid character?

Thanks in advance. J

+7
source share
2 answers

Since this has significant gaps, you need to indicate that your character's expression remains in a single solitary whitespace. See how this is done below in the dumpchar definition:

 hexdump = """\ . a . valid . string . . another . string . . etc . . . . . . . . . . . . line . w . a . space . . etc . . . . . . . . . . . . """ from pyparsing import oneOf, printables, delimitedList, White, LineEnd # expression for a single char or space dumpchar = oneOf(list(printables)+[' ']).leaveWhitespace() # convert '. 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) 

Print

 _a_valid_string_ _another_string_ _etc____________ line_w_a_ space_ _etc____________ 
+6
source

Consider another way to remove spaces

 >>> s=". a . valid . string ." >>> s=s[::2] >>> s '.a.valid.string.' 
+2
source

All Articles