如何根据文本而不是字母编号对 python 中的字符串进行切片?

How do I slice a string in python based on the text, not the letter number?

我正在尝试自动执行一些文档编辑,想知道是否有一种方法可以对此进行编程以简化它?我需要删除位于用户输入中心的大部分文本,这些文本每次都不同但被常量包围。例如,用户输入将是以下格式的 copy/paste:

"A: 测试名称在这里

B:这里是测试日期

C: 冗长的细节,这里没有必要。

D:这里总结

我想把 C 一直剪到 D 的开头得到:

"A: 测试名称在这里

B:这里是测试日期

D:这里总结

我想执行删除 ["C" : "D" -1] 之类的操作并保存其余文本。在实际文档中,“C”是一个更具体的唯一词,实际上不会删除文本中的每个字母 c。

看似简单,但我想不通。感谢您的帮助!

您可以使用 re.sub:

import re

s = """\
A: Test Name Here
B: Test Date Here
C: Long details that are unnecessary here.
D: Summary here"""

print(s)
print("---------")
print(re.sub("C:[\s\S]*?(?=D:)", "", s))

输出:

A: Test Name Here
B: Test Date Here
C: Long details that are unnecessary here.
D: Summary here
---------
A: Test Name Here
B: Test Date Here
D: Summary here

根据您指定的输出格式,这将起到作用:

import re

text = """A: Test Name Here 
B: Test Date Here
C: A looot of unnecessary staff here, including $ymbols and num3rs.
D: Summary here"""

reText = re.sub("C:.*\n* *?(?=D:)", "", text)

print(reText)

或通过打开文件:

import re

fp = open("./text.txt", "r").read()

reText = re.sub("C:.*\n* *?(?=D:)", "", fp)

print(reText)

显示的输出:

A: Test Name Here 
B: Test Date Here
D: Summary here