如何删除字符串的左侧部分? [英] How to remove the left part of a string?

查看:46
本文介绍了如何删除字符串的左侧部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些简单的 python 代码来搜索文件中的字符串,例如path=c:\path,其中 c:\path 部分可能会有所不同.当前代码为:

I have some simple python code that searches files for a string e.g. path=c:\path, where the c:\path part may vary. The current code is:

def find_path(i_file):
    lines = open(i_file).readlines()
    for line in lines:
        if line.startswith("Path="):
            return # what to do here in order to get line content after "Path=" ?

获取Path=后文本的简单方法是什么?

What is a simple way to get the text after Path=?

推荐答案

Python 3.9 开始,您可以使用 removeprefix:

Starting in Python 3.9, you can use removeprefix:

'Path=helloworld'.removeprefix('Path=')
# 'helloworld'

这篇关于如何删除字符串的左侧部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆