When using Python strings, you must constantly replace the relevant code. Some people still have many problems when using them. Next we will learn in detail. Because Python strings provide good string operation functions, you can quickly write a preliminary version:
- # Code 1
- #-*-Coding: UTF-8 -*-
- Def CutLineNum (inStr): # declare a function with the def keyword. Add a colon to the end.
- MultiStr = inStr. splitlines (1) # split multiple lines of text into text lists
- OutStr = u'' for singleStr in multiStr: # the structure of the for in loop is used, followed by a colon.
- SingleStrsingleStr = singleStr. lstrip () # Remove the left blank
- I = 0 # iteration operator for charStr in singleStr: # search for characters from left to right
- If charStr. isdigit (): # if it is determined to be a digital I + = 1 # iteration operator plus 1
- Elif I> 0: # A non-numeric character is found, and the front side has a number character
- SingleStrsingleStr = singleStr [I:] # returns the remaining characters from this position.
- Break # Skip Loop
- Else: # If none of them exist, it indicates a blank line.
- Break # Skip Loop
- OutStr + = singleStr # complete code composed of all rows
- Return outStr # return
- Note: #-*-coding: UTF-8-*-uses UTF-8 encoding and supports Chinese characters)
The above code implements Step 1-3 in the basic program framework. Step 1 is complicated because some row numbers have delimiters, some do not, and the number of spaces is not the same ), therefore, use the following functions for manual processing:
- # -*- coding: utf-8 -*-
- def CutLeftChar(inStr): outStr = u'' multiStr = inStr.splitlines(1)
for singleStr in multiStr: singleStrsingleStr = singleStr[1:]
outStr += singleStr return outStr
In this way, the replacement of Python strings is at least solved.