简体   繁体   English

从字符串中提取 substring 和 x 字符

[英]Extract a substring and x characters from a string

I have a string:我有一个字符串:

desc = 'Communication is a cornerstone of society. It is the expression of people who want to feel connected and share their thoughts, interests and feelings with one another. And telecommunication allows us to interact even when we are in different places and separated by a distance.'

If a specific substring is in desc i want to extract that substring and also the next x characters after it.如果特定的 substring 在 desc 中,我想提取 substring 以及它后面的下一个 x 字符。

Ex:前任:

if 'telecommunication' in desc :
     specific_desc = 'telecommunication allows us to interact'

if 'society' in desc:
     specific_desc = 'society. It is the expression of people who want to feel connected and 
                      share their thoughts'

I don't know how to get the x characters after the specific substring.我不知道如何获取特定 substring 之后的 x 字符。

You can do something like this, You can use index slicing.你可以做这样的事情,你可以使用索引切片。

string = 'Communication is a cornerstone of society'

substring = 'Communication'

x = 15

if substring in string:
    index = string.find(substring)
    print(string[index:index + x])

You can use the index() or find() to get the starting position of the substring, and then slicing to get the characters you want.可以使用index()或者find()得到substring的起始position,然后切片得到你想要的字符。

def get_sub_x(full, sub, x):
   try:
      index = full.index(sub)
      return full[index: index + len(sub) + x]
   except ValueError:
      return None

print(get_sub_x('Hello World', 'Wo', 2))

Outputs Worl输出Worl

Try this尝试这个

n = 15
keyword = "society"

found = re.search(f"({keyword}.*)", desc).group(0)
found = found[len(keyword) + n]
print(found)

# 'society. It is the expression of people who want to feel connected and share their thoughts, interests and feelings with one another. And telecommunication allows us to interact even when we are in different places and separated by a distance.'
def get_index (string):
    if string in desc:
        return desc.index (string)

start = get_index ('telecommunications ')
end = get_index ('even')
print (desc [start:end])

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

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