I'm looking for some like REXX PARSE to manage strings in Phyton.
Could anyone tell me if there is an extension of Phyton to manage the string parsing at least similar to REXX PARSE?
for example :
How to PARSE a string in position and length in python
Thanks
Jason Cai
Simple answer: No, there is no builtin parse equivalent where you can supply a template by which to break up a string.
There is a package parse pyPI which offers something akin to a template method of breaking a string apart, but the use of a template is as close as it gets.
You can definitely parse a string by position and length in python. The example below uses slicing which simulates position and length by specifying the starting and ending position.
In: line = 'Here is some data in a string of exactly 54 characters'
a,b,c = (line[0:4],line[5:7],line[13:17])
print(f'a is "{a}", b is "{b}", c is "{c}"')
Out: a is "Here", b is "is", c is "data"
The equivalent parse statement in rexx would be:
****** ********************************* Top of Data **********************************
000001 /* rexx */
000002
000003 line = 'Here is some data in a string of exactly 54 characters'
000004
000005 parse var line a 5 . 6 b 8 . 14 c 18 .
000006
000007 say "a is '"||a||"', b is '"||b||"', c is '"||c||"'"
000008
****** ******************************** Bottom of Data ********************************
Output:
a is 'Here', b is 'is', c is 'data'
***
I do not say this is a good way to do it, just a way. It is far better for you to learn the python way to do pythonic things rather than to approach them with a rexx mindset.