Function: String.Join ()
Python has join () and Os.path.join () two functions, which work as follows:
Join (): An array of connection strings. Generates a new string from a string, tuple, or element in the list, with the specified character (delimiter) connection
Os.path.join (): Combine multiple paths to return
First, function description
1. Join () function
Syntax: ' Sep '. Join (SEQ)
Parameter description
Sep: Delimiter. Can be empty
SEQ: sequence of elements to concatenate, strings, tuples, dictionaries
The syntax above is to combine all the SEQ elements into a new string with Sep as a delimiter
Return value: Returns a string that is generated after each element is concatenated with the delimiter Sep
2. Os.path.join () function
Syntax: Os.path.join (path1[,path2[,......])
Return value: Combine multiple paths to return
Note: Parameters before the first absolute path are ignored
Note: The element inside the container object must be a character type
#对序列进行操作(分别使用‘ ‘与‘:‘作为分隔符)
>>> seq1
=
[
‘hello‘
,
‘good‘
,
‘boy‘
,
‘doiido‘
]
>>>
print
‘ ‘
.join(seq1)
hello good boy doiido
>>>
print
‘:‘
.join(seq1)
hello:good:boy:doiido
#对字符串进行操作
>>> seq2
=
"hello good boy doiido"
>>>
print
‘:‘
.join(seq2)
h:e:l:l:o: :g:o:o:d: :b:o:y: :d:o:i:i:d:o
#对元组进行操作
>>> seq3
=
(
‘hello‘
,
‘good‘
,
‘boy‘
,
‘doiido‘
)
>>>
print
‘:‘
.join(seq3)
hello:good:boy:doiido
#对字典进行操作: dict是以Key值作连接,
#dict 的无序性,使元素随机连接。set 同理
>>> seq4
=
{
‘hello‘
:
1
,
‘good‘
:
2
,
‘boy‘
:
3
,
‘doiido‘
:
4
}
>>>
print
‘:‘
.join(seq4)
boy:good:doiido:hello
#合并目录
>>>
import
os
>>> os.path.join(
‘hello‘
,
‘goodboy‘
,
‘doiido‘
)
‘hello\goodboy\doiido‘ #windows platform auto add \ between each string
' Hello/goodboy/doiido ' #linux platform auto Add/between each string
----------------------------------------------------------------------------------------------
Function: Split ()
There are two functions for split () and Os.path.split () in Python, as follows:
Split (): Splits the string. Slices a string by specifying a delimiter and returns a segmented list of strings
Os.path.split (): Splits the file name and path by path
First, function description
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 used as an element of the list
Num: Indicates the number of splits. If there is a parameter num, it is separated into only num+1 substrings, and each substring can be assigned to a new variable
[n]: Indicates selection of Nth Shard
Note: When spaces are used as separators, 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 a parameter:
- 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
>>> u
=
"www.doiido.com.cn"
#使用默认分隔符
>>>
print
u.split()
[
‘www.doiido.com.cn‘
]
#以"."为分隔符
>>>
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‘
]
#分割两次,并取序列为1的项
>>>
print
u.split(
‘.‘
,
2
)[
1
]
doiido
#分割最多次(实际与不加num参数相同)
>>>
print
u.split(
‘.‘
,
-
1
)
[
‘www‘
,
‘doiido‘
,
‘com‘
,
‘cn‘
]
#分割两次,并把分割后的三个部分保存到三个文件
>>> u1,u2,u3
=
u.split(
‘.‘
,
2
)
>>>
print
u1
www
>>>
print
u2
doiido
>>>
print
u3
com.cn
----------------------------------------------------------------------------------------Remove line break
>>> c
=
‘‘‘say
hello
baby‘‘‘
>>>
print
c
say
hello
baby
>>>
print
c.split(
‘\n‘
)
[
‘say‘
,
‘hello‘
,
‘baby‘
]
detach 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‘
)
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‘
]
Import OS
def currentpath (file):
Try
filepath = Os.path.abspath (
Os.path.join (
Os.path.join (os.path.dirname (file), Os.pardir), Os.pardir, ' elements/' + ' IOS ' + ' _elements ')
Return filepath
Except exception,msg:
Print msg
Print __file__ #allen. py
S1 = Os.path.abspath (__file__)
Print S1 # C:\pythonn\allen.py
S2 = os.path.dirname (S1)
Print S2 # C:\pythonn
Print Os.pardir #..
Print Currentpath (__file__) # C:\elements\IOS_elements
Print os.path.join (' Hello ', ' good ', ' Boy ') # Hello\good\boy
The join () function in Python split () function