Python string ultra-detailed usage Daquan

Original link: https://www.jianshu.com/u/8f2987e2f9fb

String concatenation

Actual scene: the data list together into a string

Solution: Use str.join () method

>>> li = ['cxk', 'cxk', 'kk', 'caibi']
>>> ''.join([str(i) for i in li])
'cxkcxkkkcaibi'

We recommended to use a generator expression, if the list is large, you can save a lot of memory space

>>> ''.join(str(i) for i in li)
'3cxkkkcaibi'

Split string delimiter contains a variety of

The actual scene of: dividing a character string based on different symbol splitter field, the string comprising a plurality of different delimiter

s = "ab;fd/ft|fs,f\tdf.fss*dfd;fs:uu}fsd"

1. The python split () method, a processing time since the split separator, for example:

>>> res = s.split(';')
>>> res
['ab', 'fd/ft|fs,f\tdf.fss*dfd', 'fs:uu}fsd']

So we need to string delimiter sequence segmentation may be a map function!

>>> list(map(lambda x: x.split("|"), res))
[['ab'], ['fd/ft', 'fs,f\tdf.fss*dfd'], ['fs:uu}fsd']]

The results turned into a two-dimensional list, and the result we want is one-dimensional list, how do?

Create a temporary list to save the results.

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
>>> t = []
>>> list(map(lambda x: t.extend(x.split("|")), res))
[None, None, None]
>>> t
['ab', 'fd/ft', 'fs,f\tdf.fss*dfd', 'fs:uu}fsd']

The results met our expectations! Then continue processing the rest of the separator, the operation is repeated, for loop get!

The final code is as follows:

def my_split(s, ds):
    res = [s]

    for d in ds:
        t = []
        list(map(lambda x: t.extend(x.split(d)), res))
        res = t
    return res

The string and the string delimiters all incoming following results:

s = "ab;fd/ft|fs,f\tdf.fss*dfd;fs:uu}fsd"
print(my_split(s, ";/|,.}:*\t"))
运行结果:['ab', 'fd', 'ft', 'fs', 'f', 'df', 'fss', 'dfd', 'fs', 'uu', 'fsd']

2. re module split () method

re () also provides us with the split () method, can be a one-time-delimited string!

import re

s = "ab;fd/ft|fs,f\tdf.fss*dfd;fs:uu}fsd"
print(re.split('[;/|,.}:*\t]', s))

Results are consistent, it is not very simple and crude!

Interpretation of a string of whether the beginning or end of the string b
actual scene: for example, there is a series of files in a directory:
Here Insert Picture Description
Write a program to which all .txt files and executable .py file with user rights

solution:

String str.startswith () and str.endswith ()

And find a .txt ending .py file that accepts a tuple

>>> import os
>>> os.listdir(".")
['app', 'config', 'requirements.txt', 'run.py', '__pycache__', 'gunicorn.conf.py', 'chromedriver', 'login_after2.png', 'readme.txt', 'slide.png', 'test.py', 'logs', 'chrome-linux.zip', 'gunicorn.pid', 'asgi.py', 'chrome-linux']

>>> [name for name in os.listdir(".") if name.endswith((".txt", ".py"))]
['requirements.txt', 'run.py', 'gunicorn.conf.py', 'readme.txt', 'test.py', 'asgi.py']

Chinese string format adjustment according to the present

Actual text column: for example, in a log file, where the date format is 'yyyy-mm-dd':

We want to change the date in which the US date format 'mm / dd / yyyy'. For example 2019-06-12 06/12/2019 format into

Solution: Use of re sub () method to do the replacement string

Regular expressions in the capture group, capturing the contents of each part, and then replace the order of the respective adjustment groups captured string alternative!

code show as below:

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import re

with open("info.log", "r", encoding="utf-8") as f:
    file = f.read()

print(re.sub('(\d{4})-(\d{2})-(\d{2})', r'\2/\3/\1', file))

Capturing each group need parentheses, and then from left to right by default into groups 1, 2 ...

The next parameter is the format we want to replace, respectively, with 1,2,3 groups 1, 2 ...

Results are as follows:
Here Insert Picture Description

The string left, right, center alignment

solution:

1. string str.ljust (), str.rjust (), str.center () for about centered!
Basic usage above three methods:

>>> s = 'abc'
>>> s.ljust(20, '=')
'abc================='
>>> s.ljust(20)
'abc           '

All three methods can set default values ​​to fill

2. Use the built-in format () method

>>> format(s, ">20")
'                 abc'
>>> format(s, "<20")
'abc                 '
>>> format(s, "^20")
'        abc         '

Delete unwanted characters in the string

actual case:

Filter out the excess before and after the user enters a blank character: "[email protected]"

Editing text filter out some windows at the "\ r": "hello world \ r \ n"

unicode combined symbol text removed (pitch): nǐ hǎo mā

solution:

Use str.strip (), str.lstrip (), str.rstrip () method to remove the character string ends

Use str.replace () or regular in the re.sub ()

Use string str.translate () method, a plurality of different characters can be deleted at the same time

Guess you like

Origin blog.csdn.net/qdPython/article/details/102757612