How to replace Python strings with codes

Source: Internet
Author: User

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:

 
 
  1. # Code 1
  2. #-*-Coding: UTF-8 -*-
  3. Def CutLineNum (inStr): # declare a function with the def keyword. Add a colon to the end.
  4. MultiStr = inStr. splitlines (1) # split multiple lines of text into text lists
  5. OutStr = u'' for singleStr in multiStr: # the structure of the for in loop is used, followed by a colon.
  6. SingleStrsingleStr = singleStr. lstrip () # Remove the left blank
  7. I = 0 # iteration operator for charStr in singleStr: # search for characters from left to right
  8. If charStr. isdigit (): # if it is determined to be a digital I + = 1 # iteration operator plus 1
  9. Elif I> 0: # A non-numeric character is found, and the front side has a number character
  10. SingleStrsingleStr = singleStr [I:] # returns the remaining characters from this position.
  11. Break # Skip Loop
  12. Else: # If none of them exist, it indicates a blank line.
  13. Break # Skip Loop
  14. OutStr + = singleStr # complete code composed of all rows
  15. Return outStr # return
  16. 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:

 
 
  1. # -*- coding: utf-8 -*-  
  2. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.