Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{:d} parsing ' 0' is wrong #133

Open
chengdi1988 opened this issue Feb 24, 2021 · 1 comment
Open

{:d} parsing ' 0' is wrong #133

chengdi1988 opened this issue Feb 24, 2021 · 1 comment

Comments

@chengdi1988
Copy link

from parse import parse

parse('{a:d}','    0') # more than one blank before zero
# expect a==0
# result: None
@chengdi1988 chengdi1988 changed the title {:d} parsing ' 0' is wrong {:d} parsing ' 0' is wrong Feb 24, 2021
@jonathangjertsen
Copy link
Contributor

That is the expected behaviour. Remember that parse is intended to be the inverse of format, and there is no x such that format('{a:d}', x) returns a string that is left-padded with spaces. Therefore the corresponding call to parse must return None.

To match an arbitrary number of spaces in front, you could either use a regex of strip the string before passing it to parse.

BTW, parse does provide a result if you set the width specifier in the format string to any positive number at all:

>>> parse('{a:4d}', '    0')
<Result () {'a': 0}>
>>> parse('{a:1d}', '    0')
<Result () {'a': 0}>
>>> parse('{a:1000d}', '    0')
<Result () {'a': 0}>

But that's obviously a bug, so I wouldn't rely on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants