current position:Home>Strange Python strip

Strange Python strip

2022-02-01 16:03:07 Mourners study in the cold palace

「 This is my participation 11 The fourth of the yuegengwen challenge 8 God , Check out the activity details :2021 One last more challenge 」 Recently resolved an interface request , Sometimes you can succeed , Sometimes you fail , The focus has been on whether there is a problem with the interface provided by the product , Did they quietly change things again , Because the product is very unstable , Not necessarily , It took all morning , finally debug Here's the problem , I didn't expect it was python Of strip The function is playing tricks

Let's look at the phenomenon first

I want to get input label value The value of the property text = 'value="vrQzyLaHwrWiNdeW-hcgW-JovDCXoWhvtNA_wisDg47T5FWM9-mL_cVZ4fe_RWEzsgbTVvLOXDXFp2uzXi6z1w==">' Then I use python Of lstrip and rstrip Method removal value=" and "> But when I execute , Didn't get what I wanted , So it turns out , One more... Than expected v 'rQzyLaHwrWiNdeW-hcgW-JovDCXoWhvtNA_wisDg47T5FWM9-mL_cVZ4fe_RWEzsgbTVvLOXDXFp2uzXi6z1w=='

Troubleshooting and causes

We take it for granted that , Removing the match is a whole string , But it's not ( This is a builtin function , The source code is c Realized , There's no concrete implementation ), Through repeated experiments, it is found that , It can also be seen from the official documents , The parameter is actually a character set , Every time you remove characters , Take the characters in the character set and the beginning of the string to be removed / Ending comparison , If it matches, remove , So keep cycling , Until there is no match , end

Let's take a few examples

 picture .png analysis :

str= 'value""aluev'

strip_char_set = 'value"'

With lstrip Method , Remove... From the left loop str, Start traversing from the left :

(1)v In the character set strip_char_set, Remove v, The result of this step is 'alue""aluev'

(2) a In the character set strip_char_set, Remove v, The result of this step is 'lue""aluev' successively , The final result is an empty string

Look at this again

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

str= 'Arthur: three!'

strip_char_set = 'Arthur: '

With lstrip Method , Remove... From the left loop str, Start traversing from the left :

(1)A In the character set strip_char_set, Remove v, The result of this step is 'rthur: three!'

(2) r In the character set strip_char_set, Remove v, The result of this step is 'thur: three!' successively , Cycle all the way to e Not in the character set , The loop ends , The result is 'ee!'

Yan Shen

In fact, it's not strange at all , Or we don't learn well , What if you want to cut off the whole string , Except in regular or other ways ,python3.9 Two new functions are added , It can meet our needs , But now for some reason , It must be used. python3.7, I can only see

 picture .png

copyright notice
author[Mourners study in the cold palace],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202011603049409.html

Random recommended