Usage split python in the () function
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 ( '/ diode / soft / python /))
('/dodo/soft/python', '')
print (os.path.split ( '/ diode / soft / python'))
('/dodo/soft', 'python')
Fourth, examples
str="hello boy<[www.baidu.com]>byebye"
print(str.split("[")[1].split("]")[0])
www.baidu.com
V. Example Two
[] Parentheses indicate the value 0 is the first from left to right. -1, the first right-to-left.
() Brackets, -1 represents the number of all cut. 0 is not cut. 1 is from left to right, the first cut.
1 str="http://www.runoob.com/python/att-string-split.html" 2 print("0:%s"%str.split("/")[-1]) 3 print("1:%s"%str.split("/")[-2]) 4 print("2:%s"%str.split("/")[-3]) 5 print("3:%s"%str.split("/")[-4]) 6 print("4:%s"%str.split("/")[-5]) 7 8 print("5:%s"%str.split("/",-1)) 9 print("6:%s"%str.split("/",0)) 10 print("7:%s"%str.split("/",1)) 11 print("8:%s"%str.split("/",2)) 12 print("9:%s"%str.split("/",3)) 13 print("10:%s"%str.split("/",4)) 14 print("11:%s"%str.split("/",5)) 15 结果是: 16 17 0:att-string-split.html 18 1python 19 2 : www.runoob.com 20 3 : 21 4 : http: 22 5 [ ' http: ' , '' , ' www.runoob.com ' , ' python ' , ' to-string-split.html ' ] 23 6: [ ' http://www.runoob.com/python/att-string-split.html ' ] 24 7 [ ' http: ' , ' /www.runoob.com/python/att-string -split.html ' ] 258: [ ' http: ' , '' , ' www.runoob.com/python/att-string-split.html ' ] 26 9: [ ' http: ' , '' , ' www.runoob.com ' , ' python / to-string-split.html ' ] 27, 10 [ ' http: ' , ' ' , ' www.runoob.com ' , ' python ' , ' to-string-split.html '] 28 11:[' Http: ' , '' , ' www.runoob.com ' , ' python ' , ' to-string-split.html ' ]
Six Three examples
str = "hello boy<[www.baidu.com]>byebye"
print(str.split("[")[1].split("]")[0])
结果:www.baidu.com
print(str.split("[")[1].split("]")[0].split("."))
Results: [ 'www', 'baidu', 'com']
Added:
by os.path.split () function, so you can put a path split into two parts, the part is always the last level directory or file name:
os.path.split('/Users/michael/testdir/file.txt')
result:
('/Users/michael/testdir', 'file.txt')
os.path.splitext () so that you can directly get the file extension, often very easy:
os.path.splitext('/path/to/file.txt')
result:
('/path/to/file', '.txt')
These functions merge, split path is not required to be real directories and files, they are only to manipulate strings.