简体   繁体   English

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

[英]How to remove the left part of a string?

I have some simple python code that searches files for a string eg path=c:\path , where the c:\path part may vary.我有一些简单的 python 代码可以搜索文件中的字符串,例如path=c:\path ,其中c:\path部分可能会有所不同。 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=" ?

What is a simple way to get the text after Path= ?Path=之后获取文本的简单方法是什么?

If the string is fixed you can simply use:如果字符串是固定的,您可以简单地使用:

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

which gives you everything from position 5 on in the string (a string is also a sequence so these sequence operators work here, too).它为您提供了字符串中从位置 5 开始的所有内容(字符串也是一个序列,因此这些序列运算符也可以在这里工作)。

Or you can split the line at the first = :或者您可以在第一个=处拆分行:

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

Then param is "Path" and value is the rest after the first =.然后参数是“路径”,值是第一个 = 之后的其余部分。

Remove prefix from a string从字符串中删除前缀

# ...
if line.startswith(prefix):
   return line[len(prefix):]

Split on the first occurrence of the separator via str.partition()通过str.partition()在第一次出现分隔符时str.partition()

def findvar(filename, varname="Path", sep="=") :
    for line in open(filename):
        if line.startswith(varname + sep):
           head, sep_, tail = line.partition(sep) # instead of `str.split()`
           assert head == varname
           assert sep_ == sep
           return tail

Parse INI-like file with ConfigParser使用ConfigParser解析类似 INI 的文件

from ConfigParser import SafeConfigParser
config = SafeConfigParser()
config.read(filename) # requires section headers to be present

path = config.get(section, 'path', raw=1) # case-insensitive, no interpolation

Other options其他选项

Starting in Python 3.9 , you can use removeprefix :Python 3.9开始,您可以使用removeprefix

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

Any Python version:任何 Python 版本:

def remove_prefix(text, prefix):
    return text[len(prefix):] if text.startswith(prefix) else text

Python 3.9+蟒蛇 3.9+

text.removeprefix(prefix)

For slicing (conditional or non-conditional) in general I prefer what a colleague suggested recently;对于一般的切片(有条件或无条件),我更喜欢同事最近建议的内容; Use replacement with an empty string.使用空字符串替换。 Easier to read the code, less code (sometimes) and less risk of specifying the wrong number of characters.更容易阅读代码,更少的代码(有时)和指定错误字符数的风险更小。 Ok;好的; I do not use Python, but in other languages I do prefer this approach:我不使用 Python,但在其他语言中,我更喜欢这种方法:

rightmost = full_path.replace('Path=', '', 1)

or - to follow up to the first comment to this post - if this should only be done if the line starts with Path :或者 - 跟进这篇文章的第一条评论 - 如果这应该只在行Path开头时完成:

rightmost = re.compile('^Path=').sub('', full_path)

The main difference to some of what has been suggested above is that there is no "magic number" (5) involved, nor any need to specify both ' 5 ' and the string ' Path= ', In other words I prefer this approach from a code maintenance point of view.与上面建议的一些主要区别在于,不涉及“幻数”(5),也不需要同时指定“ 5字符串“ Path= ”,换句话说,我更喜欢这种方法一个代码维护的观点。

I prefer pop to indexing [-1] :我更喜欢pop而不是索引[-1]

value = line.split("Path=", 1).pop()

to

value = line.split("Path=", 1)[1]
param, value = line.split("Path=", 1)

Or why not或者为什么不

if line.startswith(prefix):
    return line.replace(prefix, '', 1)

The simplest way I can think of is with slicing:我能想到的最简单的方法是切片:

def find_path(i_file): 
    lines = open(i_file).readlines() 
    for line in lines: 
        if line.startswith("Path=") : 
            return line[5:]

A quick note on slice notation, it uses two indices instead of the usual one.关于切片符号的快速说明,它使用两个索引而不是通常的索引。 The first index indicates the first element of the sequence you want to include in the slice and the last index is the index immediately after the last element you wish to include in the slice.第一个索引表示您想要包含在切片中的序列的第一个元素,最后一个索引是您想要包含在切片中的最后一个元素之后的索引。
Eg:例如:

sequence_obj[first_index:last_index]

The slice consists of all the elements between first_index and last_index , including first_index and not last_index .切片由first_indexlast_index之间的所有元素last_index ,包括first_index而不是last_index If the first index is omitted, it defaults to the start of the sequence.如果省略第一个索引,则默认为序列的开头。 If the last index is omitted, it includes all elements up to the last element in the sequence.如果省略最后一个索引,则它包括序列中直到最后一个元素的所有元素。 Negative indices are also allowed.也允许负指数。 Use Google to learn more about the topic.使用 Google 了解有关该主题的更多信息。

How about..怎么样..

>>> line = r'path=c:\path'
>>> line.partition('path=')
('', 'path=', 'c:\\path')

This triplet is thehead, separator, and tail .这个三元组是head 、 separator 和 tail

>>> import re

>>> p = re.compile(r'path=(.*)', re.IGNORECASE)

>>> path = "path=c:\path"

>>> re.match(p, path).group(1)
'c:\\path'

Another simple one-liner that hasn't been mentioned here:此处未提及的另一种简单单行:

value = line.split("Path=", 1)[-1]

This will also work properly for various edge cases:这也适用于各种边缘情况:

>>> print("prefixfoobar".split("foo", 1)[-1])
"bar"

>>> print("foofoobar".split("foo", 1)[-1])
"foobar"

>>> print("foobar".split("foo", 1)[-1])
"bar"

>>> print("bar".split("foo", 1)[-1])
"bar"

>>> print("".split("foo", 1)[-1])
""
line[5:]

在前五个之后为您提供字符。

Why not using regex with escape?为什么不使用带有转义的正则表达式? ^ matches the initial part of a line and re.MULTILINE matches on each line. ^匹配一行的起始部分, re.MULTILINE匹配每一行。 re.escape ensures that the matching is exact. re.escape确保匹配是准确的。

>>> print(re.sub('^' + re.escape('path='), repl='', string='path=c:\path\nd:\path2', flags=re.MULTILINE))
c:\path
d:\path2

line[5:] will give the substring you want. line[5:]将给出你想要的子字符串。 Search the introduction and look for 'slice notation'搜索简介并查找“切片符号”

如果你知道列表推导式:

lines = [line[5:] for line in file.readlines() if line[:5] == "Path="]

removeprefix() and removesuffix() string methods added in Python 3.9 due to issues associated with lstrip and rstrip interpretation of parameters passed to them.由于与传递给它们的参数的lstriprstrip解释相关的问题,在Python 3.9 中添加了removeprefix()removesuffix()字符串方法。 Read PEP 616 for more details.阅读PEP 616了解更多详情。

# in python 3.9
>>> s = 'python_390a6'

# apply removeprefix()
>>> s.removeprefix('python_')
'390a6'

# apply removesuffix()
>>> s = 'python.exe'
>>> s.removesuffix('.exe')
'python'

# in python 3.8 or before
>>> s = 'python_390a6'
>>> s.lstrip('python_')
'390a6'

>>> s = 'python.exe'
>>> s.rstrip('.exe')
'python'

removesuffix example with a list:带有列表的removesuffix示例:

plurals = ['cars', 'phones', 'stars', 'books']
suffix = 's'

for plural in plurals:
    print(plural.removesuffix(suffix))

output:输出:

car
phone
star
book

removeprefix example with a list:带有列表的removeprefix示例:

places = ['New York', 'New Zealand', 'New Delhi', 'New Now']

shortened = [place.removeprefix('New ') for place in places]
print(shortened)

output:输出:

['York', 'Zealand', 'Delhi', 'Now']

尝试以下代码

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

I guess this what you are exactly looking for我想这正是你要找的

    def findPath(i_file) :
        lines = open( i_file ).readlines()
        for line in lines :
            if line.startswith( "Path=" ):
                output_line=line[(line.find("Path=")+len("Path=")):]
                return output_line

The pop version wasn't quite right.流行版本不太正确。 I think you want:我想你想要:

>>> print('foofoobar'.split('foo', 1).pop())
foobar

The below method can be tried.可以试试下面的方法。

def remove_suffix(string1, suffix):
    length = len(suffix)

    if string1[0:length] == suffix:
        return string1[length:]
    else:
        return string1

suffix = "hello"
string1 = "hello world"

final_string = remove_suffix(string1, suffix)
print (final_string)

without having a to write a function, this will split according to list, in this case 'Mr.|Dr.|Mrs.', select everything after split with [1], then split again and grab whatever element.无需编写函数,这将根据列表进行拆分,在本例中为 'Mr.|Dr.|Mrs.',使用 [1] 选择拆分后的所有内容,然后再次拆分并抓取任何元素。 In the case below, 'Morris' is returned.在下面的情况下,返回 'Morris'。

re.split('Mr.|Dr.|Mrs.', 'Mr. Morgan Morris')[1].split()[1]

This is very similar in technique to other answers, but with no repeated string operations, ability to tell if the prefix was there or not, and still quite readable:这在技术上与其他答案非常相似,但没有重复的字符串操作,能够判断前缀是否存在,并且仍然非常可读:

parts = the_string.split(prefix_to_remove, 1):
    if len(parts) == 2:
        #  do things with parts[1]
        pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM