`
bluky999
  • 浏览: 716066 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Python中的split 小细节

阅读更多

Python中的split有两个: str.split()   os.path.split()


1 str.split([seq [,maxsplit]])   我们最常用到的!

help 信息如下:

 

split(...)
S.split([sep [,maxsplit]]) -> list of strings

Return a list of the words in the string S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator.



小细节: 如果你的分隔符是一个或者多个空格,那么你就不必给出任何参数,即直接使用str.split() 即可!需要注意的是这种分隔方法对于中间为空的项会忽略,也就是说如果你要对多行文本逐行split(),假如你期望每行都被分隔为10列,但是如果某一列为空,那么你可能只会得到9列!这个问题怎么解决?  继续思考和等待帮助ING

 

2 os.path.split()

 

主要用于分隔文件路径;它的参数很特别,要求你给出一个文件的全路径作为参数,结果是这个文件的父目录和文件名;如果你给出一个路径分隔符;如果你给出地是一个目录名,则结果中的文件名为空!

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics