Python中的join()函数split()函数
函数:string.join()
Python中有join()和os.path.join()两个函数,具体作用如下:
join(): 连接字符串数组。将字符串、元组、列表中的元素以指定的字符(分隔符)连接生成一个新的字符串
os.path.join(): 将多个路径组合后返回
一、函数说明
1、join()函数
语法: 'sep'.join(seq)
参数说明
sep:分隔符。可以为空
seq:要连接的元素序列、字符串、元组、字典
上面的语法即:以sep作为分隔符,将seq所有的元素合并成一个新的字符串
返回值:返回一个以分隔符sep连接各个元素后生成的字符串
2、os.path.join()函数
语法: os.path.join(path1[,path2[,......]])
返回值:将多个路径组合后返回
注:第一个绝对路径之前的参数将被忽略
注:容器对象内的元素须为字符类型
#对序列进行操作(分别使用' '与':'作为分隔符)
>>> 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
----------------------------------------------------------------------------------------------
函数:split()
Python中有split()和os.path.split()两个函数,具体作用如下:
split():拆分字符串。通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(list)
os.path.split():按照路径将文件名和路径分割开
一、函数说明
1、split()函数
语法:str.split(str="",num=string.count(str))[n]
参数说明:
str: 表示为分隔符,默认为空格,但是不能为空('')。若字符串中没有分隔符,则把整个字符串作为列表的一个元素
num:表示分割次数。如果存在参数num,则仅分隔成 num+1 个子字符串,并且每一个子字符串可以赋给新的变量
[n]: 表示选取第n个分片
注意:当使用空格作为分隔符时,对于中间为空的项会自动忽略
2、os.path.split()函数
语法:os.path.split('PATH')
参数说明:
- PATH指一个文件的全路径作为参数:
- 如果给出的是一个目录和文件名,则输出路径和文件名
- 如果给出的是一个目录名,则输出路径和为空文件名
>>> 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
>>> c
=
'''say
hello
baby'''
>>>
print
c
say
hello
baby
>>>
print
c.split(
'\n'
)
[
'say'
,
'hello'
,
'baby'
]
>>>
import
os
>>>
print
os.path.split(
'/dodo/soft/python/'
)
(
'/dodo/soft/python'
, '')
>>>
print
os.path.split(
'/dodo/soft/python'
)
(
'/dodo/soft'
,
'python'
)
>>>
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'
]
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
Python中的join()函数split()函数的更多相关文章
- python 中的os.path.split()函数用法
基本概念 os.path.split()通过一对链表的头和尾来划分路径名.链表的tail是是最后的路径名元素.head则是它前面的元素. 举个例子: path name = '/home/User ...
- Python中的__init__()和__call__()函数
Python中的__init__()和__call__()函数 在Python的class中有一些函数往往具有特殊的意义.__init__()和__call__()就是class很有用的两类特殊的函数 ...
- python中实现延时回调普通函数示例代码
python中实现延时回调普通函数示例代码 这篇文章主要给大家介绍了关于python中实现延时回调普通函数的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的 ...
- Python中的startswith和endswith函数使用实例
Python中的startswith和endswith函数使用实例 在Python中有两个函数分别是startswith()函数与endswith()函数,功能都十分相似,startswith()函数 ...
- Python中的"缝合器"zip函数:将多个可迭代对象组合成一个迭代器
zip函数将参数中多个可迭代对象中相同序号的元素取出组合成一个元组作为输出列表的一个同样序号的元素,即输出列表的每个元素是一个元组,该元组的元素来源于参数中每个迭代对象的对应序号的元素. 具体可参考: ...
- python中join和split函数
一个是分割,一个是连接. 惯例,先看内部帮助文档 Help on method_descriptor: join(...) S.join(iterable) -> string Return a ...
- 解释python中join()和split()函数
join能让我们将指定字符添加至字符串中 a=') print(a) print(type(a)) #1,2,3,4,5,6 #<class 'str'> split()能让我们用指定字符 ...
- Python基本知识 os.path.join与split() 函数
Python中有join和os.path.join()两个函数,具体作用如下: join:连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串os.path.joi ...
- Python中的join()函数的用法
函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字 ...
随机推荐
- mysql同步
已安装好mysql,并且已把配置文件复制在/etc/my.cnf 主服务器的配置: 1,查看二进制日志的状态,开启二进制日志 进入mysql安装目录 #cd /usr/local/mysql 进入my ...
- 文件输出debug
file_put_contents('/tmp/heat.log', var_export($mainArr, true),FILE_APPEND);
- 实现LUT
//实现LUT 查找表 #include "stdafx.h" #include <iostream> #include <opencv2/opencv.hpp& ...
- selenium实例
代码: # -*- coding:utf- -*- from selenium import webdriver driver = webdriver.PhantomJS() driver.get(' ...
- Unix网络编程--卷一:套接字联网API
UNIX网络编程--卷一:套接字联网API 本书面对的读者是那些希望自己编写的程序能够使用成为套接字(socket)的API进行彼此通信的人. 目录: 0.准备环境 1.简介 2.传输层:TCP.UD ...
- 触发onSaveInstanceState和onRestoreInstanceState的时机
先看Application Fundamentals上的一段话: Android calls onSaveInstanceState() before the activity becomes ...
- 2016年12月2日 星期五 --出埃及记 Exodus 20:23
2016年12月2日 星期五 --出埃及记 Exodus 20:23 Do not make any gods to be alongside me; do not make for yourselv ...
- out 传值
public void Out(out int a, out int b) {//out相当于return返回值 //可以返回多个值 //拿过来变量名的时候,里面默认为空值 a=1; b=2; } s ...
- Spring源码之SimpleAliasRegistry解读(一)
Spring源码之SimpleAliasRegistry解读(一) 阅读spring源码中org.springframework.core.SimpleAliasRegistry类时发现该类主要是使用 ...
- oracle分组统计某列逗号隔开数据
所有版本的oracle都可以使用select wm_concat(name) as name from user; 但如果是oracle11g,使用select listagg(name, ',') ...