1

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
  • http://stackoverflow.com/questions/761804/trimming-a-string-in-python – McNets Dec 15 '16 at 17:47
  • That post is about removing leading and trailing spaces with `strip()`, but I'm interested also in ignoring spaces in the middle of the template, as well as in a solution which uses `parse`. – Kurt Peek Dec 15 '16 at 17:54
  • try with `.replace()` https://www.tutorialspoint.com/python/string_replace.htm – McNets Dec 15 '16 at 17:59

3 Answers3

2

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') {}>
Drew Pierce
  • 214
  • 1
  • 2
  • 7
1

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.

AlG
  • 14,697
  • 4
  • 41
  • 54
1

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') {}>
martineau
  • 119,623
  • 25
  • 170
  • 301