“This is my 8th day of the November Gwen Challenge. 2021 the last time the more challenge “parse an interface request recently, sometimes successfully, sometimes fail, concerns have been products to me if there is problem, provide the interface they are quietly changed things, because the product is not very stable, nor will now, just spent the whole morning today, finally the debug the problem, It was the Python strip function that did the trick

Let’s look at the phenomenon first

I want to get the value of the input tag value property text = ‘value=”vrQzyLaHwrWiNdeW-hcgW-JovDCXoWhvtNA_wisDg47T5FWM9-mL_cVZ4fe_RWEzsgbTVvLOXDXFp2uzXi6z1w==”>’ And then I used Python’s lstrip and rstrip methods to get rid of value=” and “> but when I did that, I didn’t get what I wanted, so here’s the result, Compared to the expected more removed a v ‘rQzyLaHwrWiNdeW hcgW – JovDCXoWhvtNA_wisDg47T5FWM9 – mL_cVZ4fe_RWEzsgbTVvLOXDXFp2uzXi6z1w = =’

Troubleshooting and Causes

We take it for granted that the removal matches a whole string, but this is not the case (this is the builtin function, the source code is c, not the concrete implementation). Through repeated experiments and official documentation, we found that the parameter is actually a character set, and every time we remove a character, Each character in the character set is compared to the beginning/end of the string to be removed, and if there is a match, it is removed, and so on until there is no match, and the cycle ends

Let’s look at a couple of examples

Resolution:

str= ‘value””aluev’

strip_char_set = ‘value”‘

Using the lstrip method, remove the loop STR from the left and iterate from the left:

(1)v in character set strip_char_set, remove v, this step result is ‘alue””aluev’

(2) a in character set strip_char_set, remove v, this step result is ‘lue””aluev’ order, the final result is an empty string

Look at this

'Arthur: three! '.lstrip('Arthur: ') 'ee! 'Copy the code

str= ‘Arthur: three! ‘

strip_char_set = ‘Arthur: ‘

Using the lstrip method, remove the loop STR from the left and iterate from the left:

‘rthur: three! ‘strip_char_set ‘

‘thur: three! ‘when r is in character set strip_char_set ‘until e is not in the character set, the loop ends, and the result is’ EE! ‘

Python3.9 adds two new functions that satisfy our needs, but now I have to use python3.7 for some reason, so I’ll just have to look at them. Okay