Usage and explanation of strip().split('\t') in Python

content

(1), the usage of strip()

 (2), the usage of split

(3) Usage of strip().split()


(1), the usage of strip()

strip() is mainly to operate on strings, mainly to delete the first and last characters of the string you currently get. If you do not specify characters in the parentheses of strip(), that is, if the parentheses are empty, then the default will be Remove leading and trailing spaces and newlines from the current string.

An example is as follows:

1. Delete the preceding spaces

2. Remove leading and trailing spaces and newlines

3. Delete the specified characters at the beginning and end

 (2), the usage of split

        The split function usually operates on strings, and the result after the operation becomes a list of strings. The split function splits according to the characters given in the parentheses. If the parentheses are empty, that is, if no specific splitting content is specified, then the default splitting is based on spaces.

       The specific usage of split() is as follows:

1. Divide according to the default method (the default is to divide according to spaces)

2. Split according to a specified character, and return a string list after splitting. The string list does not include the delimiter you specified. For example, in the following example, the letter d is not included

 3. It can also be divided according to '\n', '\t'

(3) Usage of strip().split()

        After the previous two demonstrations, we should be able to roughly understand that this combined usage is usually used in the read division of files. That is, after reading in a certain format, it is divided, and the redundant characters at the beginning and the end are deleted. Let's look at an example.

Suppose you are given a document (.txt format), the content is as follows, nine lines of data, and there are several spaces between each line.

 

We first open the document and then read it, and then call the joint function, let's take a look at the effect.

1. First, let's read the file line by line. The code is as follows.

if __name__ == '__main__':
    fr = open('lenses.txt')   #文件名为:lenses.txt
    fp = fr.readlines()       #按行读取
    print(fp)
    #fp打印的结果格式如下:我展示的只是前三行的数据
    #['young\tmyope\tno\treduced\tno lenses\n', 'young\tmyope\tno\tnormal\tsoft\n', 'young\tmyope\tyes\treduced\tno lenses\n']

2. Observe the result of reading the file by line. We see that each line is read as a string. This string contains the space character '\t' and the newline character '\n' , so we are here The goal of the first step is to remove the space character '\t' and call the split() function.

code show as below:

# LBS
# 日期:2022/3/4 11:01
from sklearn import tree

if __name__ == '__main__':
    fr = open('lenses.txt')
    fp = fr.readlines()
    print(fp[0].split('\t'))
    #打印出第一行经过处理的结果为:['young', 'myope', 'no', 'reduced', 'no lenses\n']

3. We see that after split processing, there is a newline '\n' at the end of the first line. The function of strip() we just mentioned is to remove spaces and newlines before and after the string.

code show as below:

# LBS
# 日期:2022/3/4 11:01
from sklearn import tree

if __name__ == '__main__':
    fr = open('lenses.txt')
    fp = fr.readlines()
    print(fp[0].strip().split('\t'))
    #打印出第一行经过处理的结果为:['young', 'myope', 'no', 'reduced', 'no lenses']

At this time, we can see that after the data in the first line is processed by the joint function strip().split(), the spaces and line breaks are removed, which is more convenient to use. If you want to process multiple rows of data together, you need to use a for loop

for example:

fp.strip().split('\t') for fp in fr.readlines()

 If you have a better understanding, please leave a message to communicate and learn from each other.

If reproduced. Please declare the source, it is not easy to organize, please like it.

 

Guess you like

Origin blog.csdn.net/BaoITcore/article/details/123277521