split(),strip,split("/")[-1] 和 split("/",-1)的区别,pandas中的split
split( )函数用来切割str字符串,返回一个ndarray类型的数据。
1 | str .split( "分割符" , 分割次数) |
“分隔符”:为默认值时,认为空格、\n、\t等都是分隔符;
分割次数:默认是-1,即在所有的分隔符都进行分割,当num=1时表示对str只分割一次,num=2时表示对str分割两次,以此类推。
1 2 3 4 5 | a = "123 \n456 \n789" print (a.split()) #Out:['123', '456', '789'] print (a.split( ' ' , 2 )) #分隔符是空格 #Out:['123', '\n456', '\n789'] |
split()[i] 函数用来获取str中第i个分隔符和第i+1个分隔符之间的内容,返回一个ndarray类型的数据。
1 2 3 4 5 6 7 8 9 10 11 | b = "123 \n 456 \n789 \n" print (b.split( "\n" )[ 1 ]) #Out:[' 456 '] print (b.split( "\n" )[ 2 ]) #Out:['789 '] b = "123 \n 456789 \t91011" print (b.split( "\n" )[ 1 ].split( '\t' )[ 0 ]) #Out:[' 456789 '] |
此外,可参考:https://blog.csdn.net/zhouzongxin94/article/details/108094311
Python中split()函数,通常用于将字符串切片并转换为列表。
一、函数说明:
split():语法:
1 | str .split( str = "",num = string.count( str ))[n] |
拆分字符串。通过制定分隔符将字符串进行切片,并返回分割后的字符串列表[list]
参数:str:分隔符,默认为空格,但不能为空("")
num: 表示分割次数。如果指定num,则分割成n+1个子字符串,并可将每个字符串赋给新的变量
1 2 3 4 5 | u = "www.google.com" print (u.split( '.' )) - - > [ 'www' , 'google' , 'com' ] print (u.split( '.' )[ 0 ]) - - >www |
三、补充
line.strip().split(’,’)
strip()表示删除掉数据中的换行符,split(‘,’)则是数据中遇到‘,’ 就隔开。
用strip可以去除头和尾的空格,对于字符串中间的空格用.join(split())去掉
函数: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[,......]])
返回值:将多个路径组合后返回
注:第一个绝对路径之前的参数将被忽略
二、实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #对序列进行操作(分别使用' '与':'作为分隔符) >>> 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' |
函数: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')
参数说明:
1.PATH指一个文件的全路径作为参数:
2.如果给出的是一个目录和文件名,则输出路径和文件名
3.如果给出的是一个目录名,则输出路径和为空文件名
二、分离字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | string = "www.gziscas.com.cn" 1. 以 '.' 为分隔符 print (string.split( '.' )) [ 'www' , 'gziscas' , 'com' , 'cn' ] 2. 分割两次 print (string.split( '.' , 2 )) [ 'www' , 'gziscas' , 'com.cn' ] 3. 分割两次,并取序列为 1 的项 print (string.split( '.' , 2 )[ 1 ]) gziscas 4. 分割两次,并把分割后的三个部分保存到三个文件 u1, u2, u3 = string.split( '.' , 2 ) print (u1)—— www print (u2)—— gziscas print (u3) ——com.cn 三、分离文件名和路径 import os print (os.path.split( '/dodo/soft/python/' )) ( '/dodo/soft/python' , '') |
三、分离文件名和路径
1 2 3 4 5 | import os print (os.path.split( '/dodo/soft/python/' )) ( '/dodo/soft/python' , '') |
四、实例


作者:空口言_1d2e
链接:https://www.jianshu.com/p/85ac0af2c30e
参考链接:https://www.jianshu.com/p/c7bb1c26840c
转载自:https://blog.csdn.net/jialibang/article/details/84989279
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | str = "http://www.runoob.com/python/att-string-split.html" print ( "-1:%s" % str .split( "/" )[ - 1 ]) print ( "-2:%s" % str .split( "/" )[ - 2 ]) print ( "-3:%s" % str .split( "/" )[ - 3 ]) print ( "-4:%s" % str .split( "/" )[ - 4 ]) print ( "-5:%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 )) #输出: - 1 :att - string - split.html - 2 :python - 3 :www.runoob.com - 4 : - 5 :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'] |
即: -1 :全切 ;
0 :不切 ;
1:切一刀 (每一块都保留);
[-1]:将最后一块切割出来 [-2]:将倒数第二块切割出来 (只保留切出来的一块)
pandas中的split
一、函数详解
split()左分列,rsplit()右分列
df['A'].str.split(pat = None,n = -1,expand = False )
pat:str,要拆分的字符串或正则表达式。如果未指定,则拆分空格。
n:int,默认-1;限制输出中的分割数。None,0和-1被解释为返回所有拆分。【就是当用于分列的依据符号在有多个的话需要指定分列的次数(不指定的话就会根据符号有几个分列几次)】
expand:bool,默认为False;将拆分的字符串展开为单独的列。
True:返回DataFrame / MultiIndex;
False:返回包含字符串列表的Series / Index。
rsplit()类似!!!
二、举例
二、举例
1、以Order_ID为例
2、未设置n和expand——分列全部,为list。
3、分列左边的1个,返回DataFrame
4、从右边分列
原文链接:https://blog.csdn.net/hhq2lcl/article/details/84699591
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)