gpt4 book ai didi

python - 如何删除字符串的左侧部分?

转载 作者:IT老高 更新时间:2023-10-28 12:35:00 25 4
gpt4 key购买 nike

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

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=之后的文本的简单方法是什么?

最佳答案

如果字符串是固定的,你可以简单地使用:

if line.startswith("Path="):
return line[5:]

它为您提供字符串中从位置 5 开始的所有内容(字符串也是一个序列,因此这些序列运算符也可以在这里工作)。

或者你可以在第一个=处分行:

if "=" in line:
param, value = line.split("=",1)

然后参数是“路径”,值是第一个 = 之后的其余部分。

关于python - 如何删除字符串的左侧部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/599953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com