Function: Split ()
There are split () and Os.path.split () two functions in Python, which can be specified as follows:
Split (): Split string. Slices a string by specifying a delimiter, and returns a list of separated strings (list)
Os.path.split (): Split file name and path by path
I. Description of the function
1. Split () function
Syntax: Str.split (str= "", Num=string.count (str)) [n]
Parameter description:
STR: is represented as a delimiter, the default is a space, but cannot be empty ('). If there is no delimiter in the string, the entire string is taken as an element of the list
Num: Indicates the number of splits. If parameter num exists, it is separated into only num+1 substrings, and each substring can be assigned to a new variable
[n]: = select nth Fragment
Note: When you use a space as a separator, items that are empty in the middle are automatically ignored
2, Os.path.split () function
Syntax: Os.path.split (' path ')
Parameter description:
- Path refers to the full path of a file as an argument:
- If a directory and filename are given, the output path and file name
- If a directory name is given, the output path and the empty file name
Second, the example
1. Common examples
>>> u = "www.doiido.com.cn"
#使用默认分隔符
>>> print u.split ()
[' www.doiido.com.cn ']
#以 "." As separator
>>> print u.split ('. ')
[' www ', ' doiido ', ' com ', ' cn ']
#分割0次
>>> Print u.split ('. ', 0)
[' www.doiido.com.cn ']
#分割一次
>>> print u.split (' . ', 1
] [' www ', ' doiido.com.cn ']
#分割两次
>>> print u.split ('. ', 2)
[' www ', ' doiido ', ' Com.cn ']
#分割两次, with entries of 1
>>> print u.split ('. ', 2) [1]
Doiido
#分割最多次 (actually the same as no num parameter)
>>> print U.split ('. ', -1)
[' www ', ' doiido ', ' com ', ' cn ']
#分割两次, and save the segmented three sections to three files
>>> u1,u2,u3 = U.split ('. ', 2)
>>> print u1
www
> >> print U2
doiido
>>> print U3
com.cn
2, remove line break
>>> C = ' Say
Hello baby '
>>> print c
say
hello
baby
> >> print c.split (' \ n ')
[' Say ', ' hello ', ' baby ']
3, separate file name and path
>>> import os
>>> print os.path.split ('/dodo/soft/python/')
('/dodo/soft/python ', ')
>>> Print os.path.split ('/dodo/soft/python ')
('/dodo/soft ', ' python ')
4, a Super good example
>>> str= "Hello boy<[www.doiido.com]>byebye"
>>> print Str.split ("[") [1].split ("]") [0]
www.doiido.com
>>> print Str.split ("[") [1].split ("]") [0].split ("."
) [' www ', ' doiido ', ' com ']