python 新手请教,用python取字符串中最后的一个特定字符之前的字符串,谢谢了 python取字符串中最后的一个特定字符之后的字符串

python\u4e2d\u5982\u4f55\u53d6\u51fa\u5b57\u7b26\u4e32\u4e2d\u6700\u540e\u4e00\u4e2a\u7279\u5b9a\u5b57\u7b26\u540e\u7684\u5b57\u7b26\u4e32

#\u5728a\u4e2d\u67e5\u627e\u6700\u540e\u4e00\u4e2ab\u540e\u9762\u7684\u5b57\u7b26
a="ABCDEFGHIJABCDEFGHIJABCDEFGHIJ"
b="H"
print(a[a.rfind(b):])
\u6216\uff1a
str = "addhakshdaskhdsak-----12345.0"
str.find("12345.0") \u627e\u5230"12345.0"\u6240\u5728\u4f4d\u7f6e\u7684index
str[str.find("12345.0") - 1]\u5373\u662f\u9700\u8981\u7684\u5b57\u7b26

\u6269\u5c55\u8d44\u6599\uff1a
\u901a\u5e38\u4ee5\u4e32\u7684\u6574\u4f53\u4f5c\u4e3a\u64cd\u4f5c\u5bf9\u8c61\uff0c\u5982\uff1a\u5728\u4e32\u4e2d\u67e5\u627e\u67d0\u4e2a\u5b50\u4e32\u3001\u6c42\u53d6\u4e00\u4e2a\u5b50\u4e32\u3001\u5728\u4e32\u7684\u67d0\u4e2a\u4f4d\u7f6e\u4e0a\u63d2\u5165\u4e00\u4e2a\u5b50\u4e32\u4ee5\u53ca\u5220\u9664\u4e00\u4e2a\u5b50\u4e32\u7b49\u3002\u4e24\u4e2a\u5b57\u7b26\u4e32\u76f8\u7b49\u7684\u5145\u8981\u6761\u4ef6\u662f\uff1a\u957f\u5ea6\u76f8\u7b49\uff0c\u5e76\u4e14\u5404\u4e2a\u5bf9\u5e94\u4f4d\u7f6e\u4e0a\u7684\u5b57\u7b26\u90fd\u76f8\u7b49\u3002\u8bbep\u3001q\u662f\u4e24\u4e2a\u4e32\uff0c\u6c42q\u5728p\u4e2d\u9996\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u7684\u8fd0\u7b97\u53eb\u505a\u6a21\u5f0f\u5339\u914d\u3002\u4e32\u7684\u4e24\u79cd\u6700\u57fa\u672c\u7684\u5b58\u50a8\u65b9\u5f0f\u662f\u987a\u5e8f\u5b58\u50a8\u65b9\u5f0f\u548c\u94fe\u63a5\u5b58\u50a8\u65b9\u5f0f\u3002
\u53c2\u8003\u8d44\u6599\u6765\u6e90:\u767e\u5ea6\u767e\u79d1-\u5b57\u7b26\u4e32

import retext = "addhakshdaskhdsak----------------------------------12345.0"m = re.search(r'[^-]+$', text)if m: print m.group(0)else: print 'not search' \u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5b9e\u73b0\uff0c\u627e\u51fa\u6700\u540e\u7684\u591a\u4e2a\u975e-\u5185\u5bb9

s = "C:/Python27/1/3.py"
pos = s.rfind("/")
s[:pos] # "C:/Python27/1"


os.path.split(path)
可以将C:/Python27/1/3.py分解成C:/Python27/1和3.py

str="C:/Python27/1/3.py"
answer=str[:str.rindex("/3.py")]

在百度上搜索就知道了!

扩展阅读:python手机版下载官方 ... c++和python先学哪个 ... 学python后到底能干什么 ... python基础代码大全 ... python解释器手机版下载 ... python初学编程必背 ... python和c++哪个更值得学 ... python代码自动生成器 ... python新手入门零基础 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网