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
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
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
The sidebar is recommended
- Python learning notes - the fifth bullet * class & object oriented
- Python learning notes - the fourth bullet IO operation
- Python crawler actual combat: crawl all the pictures in the answer
- Quick reference manual of common regular expressions, necessary for Python text processing
- [Python] the characteristics of dictionaries and collections and the hash table behind them
- Python crawler - fund information storage
- Python crawler actual combat, pyteseract module, python realizes the visualization of boos direct employment & hook post data
- Pit filling summary: Python memory leak troubleshooting tips
- Python code reading (Chapter 61): delaying function calls
- Through the for loop, compare the differences between Python and Ruby Programming ideas
guess what you like
-
leetcode 1606. Find Servers That Handled Most Number of Requests(python)
-
leetcode 1611. Minimum One Bit Operations to Make Integers Zero(python)
-
06python learning notes - reading external text data
-
[Python] functions, higher-order functions, anonymous functions and function attributes
-
Python Networkx practice social network visualization
-
Data analysis starts from scratch, and pandas reads and writes CSV data
-
Python review (format string)
-
[pandas learning notes 01] powerful tool set for analyzing structured data
-
leetcode 147. Insertion Sort List(python)
-
apache2. 4 + windows deployment Django (multi site)
Random recommended
- Python data analysis - linear regression selection fund
- How to make a python SDK and upload and download private servers
- Python from 0 to 1 (day 20) - basic concepts of Python dictionary
- Django -- closure decorator regular expression
- Implementation of home page and back end of Vue + Django tourism network project
- Easy to use scaffold in Python
- [Python actual combat sharing] I wrote a GIF generation tool, which is really TM simple (Douluo continent, did you see it?)
- [Python] function decorators and common decorators
- Explain the python streamlit framework in detail, which is used to build a beautiful data visualization web app, and practice making a garbage classification app
- Construction of the first Django project
- Python crawler actual combat, pyecharts module, python realizes the visualization of river review data
- Python series -- web crawler
- Plotly + pandas + sklearn: shoot the first shot of kaggle
- How to learn Python systematically?
- Analysis on several implementations of Python crawler data De duplication
- leetcode 1616. Split Two Strings to Make Palindrome (python)
- Python Matplotlib drawing violin diagram
- Python crawls a large number of beautiful pictures with 10 lines of code
- [tool] integrated use of firebase push function in Python project
- How to use Python to statistically analyze access logs?
- How IOS developers learn Python Programming 22 - Supplement 1
- Python can meet any API you need
- Python 3 process control statement
- The 20th of 120 Python crawlers, 1637. All the way business opportunity network joined in data collection
- Datetime of pandas time series preamble
- How to send payslips in Python
- [Python] closure and scope
- Application of Python Matplotlib color
- leetcode 1627. Graph Connectivity With Threshold (python)
- Python thread 08 uses queues to transform the transfer scenario
- Python: simple single player strange game (text)
- Daily python, chapter 27, Django template
- TCP / UDP communication based on Python socket
- Use of pandas timestamp index
- leetcode 148. Sort List(python)
- Confucius old book network data collection, take one anti three learning crawler, python crawler 120 cases, the 21st case
- [HTB] cap (datagram analysis, setuid capability: Python)
- How IOS developers learn Python Programming 23 - Supplement 2
- How to automatically identify n + 1 queries in Django applications (2)?
- Data analysis starts from scratch. Pandas reads HTML pages + data processing and analysis