Kurt Peek
Kurt Peek

Reputation: 57421

In parse module, how to strip spaces from the parsed value?

The parse module is a useful as an inverse of format. Its documentation mentions (referring to the format specification mini-language) that

The align operators will cause spaces (or specified fill character) to be stripped from the parsed value. The width is not enforced; it just indicates there may be whitespace or "0"s to strip.

I would like to do something like this, but I wasn't able to find any examples of how to get this to work. For example, in its normal operation, if I use a template "{}{}" to match "ab", I get a Result object which contains 'a' and 'b':

In [1]: import parse

In [2]: parse.parse("{}{}","ab")
Out[2]: <Result ('a', 'b') {}>

However, if I put a space between a and b in the string, then that space alters the Result:

In [3]: parse.parse("{}{}","a b")
Out[3]: <Result ('a', ' b') {}>

I would like the parse.parse method to ignore this space and still return 'a' and 'b'. Is this possible and if so, how can I do it?

Upvotes: 1

Views: 543

Answers (3)

Drew Pierce
Drew Pierce

Reputation: 234

You'd just need to replace {} with {:^} to parse format string. Based on your examples, this worked based my testing.

{:^} strips leading and trailing whitespace

In [1]: import parse

In [2]: parse.parse("{:^}{:^}","ab")
Out[2]: <Result ('a', 'b') {}>

In [3]: parse.parse("{:^}{:^}","a b")
Out[3]: <Result ('a', 'b') {}>

In [4]: parse.parse("{:^}{:^}","a    b"
Out[4]: <Result ('a', 'b') {}>    

In [5]: parse.parse("{:^}{:^}"," a  b ")
Out[5]: <Result ('a', 'b') {}>

Upvotes: 2

martineau
martineau

Reputation: 123423

Specifying a string length (and the space between the values) seems to work:

import parse

result = parse.parse("{1s} {1s}","a b")
print(result)  # -> <Result ('a', 'b') {}>

Upvotes: 1

AlG
AlG

Reputation: 15157

If you scroll down in the documentation you linked there are explicit examples:

And messing about with alignment:

>>> parse('with {:>} herring', 'with     a herring')
<Result ('a',) {}>
>>> parse('spam {:^} spam', 'spam    lovely     spam')
<Result ('lovely',) {}>

Note that the "center" alignment does not test to make sure the value is centered - it just strips leading and trailing whitespace.

Upvotes: 1

Related Questions