码迷,mamicode.com
首页 > 编程语言 > 详细

Python

时间:2019-09-13 01:03:41      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:替换   存在   pattern   ring   prism   join()   方法   字符串拼接   strip   

sub(pattern, repl, string, count)

第一个参数:规则

第二个参数:替换后的字符串

第三个参数:字符串

第四个参数:替换个数。默认为0,表示每个匹配项都替换

 

re.sub("^\s*|\s*$","",字符串)两端
re.sub("^\s*","",字符串)首
re.sub("\s*$","",字符串)尾

 

# 遍历文件夹下面的所有文件及文件夹,如果是文件,则...如果是文件夹,则...
for i in os.listdir(path):
if os.path.isfile(os.path.join(path,i)):
print(os.path.join(path,i),"文件")
elif os.path.isdir(os.path.join( path,i)):
print(os.path.join(path,i),"文件夹")

 

# os.path.exists()方法可以直接判断文件/文件夹是否存在
# os.path.isfile("test-data") 判断 test-data 是否文件
# os.path.isdir("hello") 判断 hello是否文件夹
# join:字符串拼接
# os.path.join() 路径拼接
.strip()去除两边空白字符
.lstrip()左边字符
.rstrip()右边

Python

标签:替换   存在   pattern   ring   prism   join()   方法   字符串拼接   strip   

原文地址:https://www.cnblogs.com/chargeworld/p/11515638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!