您的位置:首页 > 编程语言 > Python开发

python中字符串拆分与合并(split()和join())

2018-03-31 22:38 766 查看

Python3 split()方法

描述

split()通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串

语法

split()方法语法:

str.split(str="", num=string.count(str))

参数

str – 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。

num – 分割次数。

返回值

返回分割后的字符串列表。

实例

以下实例展示了split()函数的使用方法:

#!/usr/bin/python3

str = "this is string example....wow!!!"
print (str.split( ))
print (str.split('i',1))
print (str.split('w'))

以上实例输出结果如下:

['this', 'is', 'string', 'example....wow!!!']
['th', 's is string example....wow!!!']
['this is string example....', 'o', '!!!']




Python3 字符串


注:split()分割字符串返回的是列表

利用re模块分割含有多种分割符的字符串:

import re
a='Beautiful, is; better*than\nugly'
# 四个分隔符为:,  ;  *  \n
x= re.split(',|; |\*|\n',a)
print(x)


应用:网页地址解析

#coding=utf-8

str="http://www.runoob.com/python/att-string-split.html"
print("0:%s"%str.split("/")[-1])
print("1:%s"%str.split("/")[-2])
print("2:%s"%str.split("/")[-3])
print("3:%s"%str.split("/")[-4])
print("4:%s"%str.split("/")[-5])

print("5:%s"%str.split("/",-1))
print("6:%s"%str.split("/",0))
print("7:%s"%str.split("/",1))
print("8:%s"%str.split("/",2))
print("9:%s"%str.split("/",3))
print("10:%s"%str.split("/",4))
print("11:%s"%str.split("/",5))


结果为

0:att-string-split.html
1:python
2:www.runoob.com
3:
4:http:
5:['http:', '', 'www.runoob.com', 'python', 'att-string-split.html']
6:['http://www.runoob.com/python/att-string-split.html']
7:['http:', '/www.runoob.com/python/att-string-split.html']
8:['http:', '', 'www.runoob.com/python/att-string-split.html']
9:['http:', '', 'www.runoob.com', 'python/att-string-split.html']
10:['http:', '', 'www.runoob.com', 'python', 'att-string-split.html']
11:['http:', '', 'www.runoob.com', 'python', 'att-string-split.html']


处理表格文件

date.txt文件如下

1 0.0888 201 36.02 28 0.5885

2 0.1399 198 39.32 30 0.8291



import os
data = []
for lines in open(r"date.dat",'r').readlines():
lines.strip()
s = [x for x in lines.strip().split()]
data.append(s)

print(data)
print(len(data[0]))


Python splitlines()方法

Python splitlines() 按照行(‘\r’, ‘\r\n’, \n’)分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符

str.splitlines([keepends])

keepends – 在输出结果里是否去掉换行符(‘\r’, ‘\r\n’, \n’),默认为 False,不包含换行符,如果为 True,则保留换行符。

str1 = 'ab c\n\nde fg\rkl\r\n'
print str1.splitlines();

str2 = 'ab c\n\nde fg\rkl\r\n'
print str2.splitlines(True)

#返回结果
['ab c', '', 'de fg', 'kl']
['ab c\n', '\n', 'de fg\r', 'kl\r\n']


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

#对字典进行操作

>>> seq4 = {'hello':1,'good':2,'boy':3,'doiido':4}
>>> print ':'.join(seq4)
boy:good:doiido:hello

#合并目录

>>> import os
>>> os.path.join('/hello/','good/boy/','doiido')
'/hello/good/boy/doiido'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: