Function: split ()
Python, the split () and The os.path.split () two functions, the following specific role:
split (): Split string. Delimiter designated by slicing the string, and returns the string list (list) after division
os.path.split (): according to the path and file name separated path
First, Function Description
1, split () function
Syntax: str.split (str = "", num = string.count (str)) [n]
Parameters:
STR: expressed as a separator, a space by default, but can not be empty ( ''). If no delimiter string, the whole string as put a list of elements
num: indicates the segment number. If the parameter num is present, only num + 1 is divided into sub-strings, each sub-string and can be assigned to a new variable
[n]: represents the n-th slice select
NOTE: When using space as delimiter, the intermediate entry is empty automatically ignore
2, os.path.split () function
Syntax: os.path.split ( 'PATH')
Parameter Description:
1.PATH refers to the full path of a file as an argument:
2. If a given directory and file name, the output path and file name
3. If a directory name is given, the output path and file name is empty
Second, the isolated string
string = " www.gziscas.com.cn "
1. '' is a delimiter
print(string.split(’.’))
[ 'Www', 'gziscas', 'with', 'cn']
2. split twice
print(string.split(’.’,2))
[ 'www', 'gziscas' ' com.cn ']
3. The split twice, and taking a sequence of items 1
print(string.split(’.’,2)[1])
gziscas
4. split twice and saves the three portions divided into three files
u1, u2, u3 =string.split(’.’,2)
print(u1)—— www
print(u2)—— gziscas
print (U3) - com.cn
Third, the separation of the file name and path
import os
print (os.path.split ( '/ dodo / soft / python /'))
( '/ Dodo / soft / python', '') # here because it is only to the catalog, so after splitting the file is empty
print (os.path.split ( '/ dodo / soft / python'))
( '/ Dodo / soft', 'python') # here to the directory and file name, python is the file name
Fourth, examples
str=“hello boy<[www.baidu.com]>byebye”
print(str.split("[")[1].split("]")[0])