【发布时间】:2021-09-18 10:25:07
【问题描述】:
有谁知道我如何从文本文件中提取已知字符串“name:”并提取最长可达 12 个字符的相关值?
例如,字符串在文件“test”中并包含:
h2. abc value\r\n * Name: name123\r\n\r\ details more words
如果键 Name: 始终相同但值 name123 一直在变化,我将如何仅提取值 Name: name123?
import re
import sys
import string
with open('file') as theFile
r = theFile.read()
print(r)
theName = re.compile(r'Name:')
mod1 = theName.search(r)
print(mod1)
输出:
<re.Match object; span(23, 33) match='Name:'>
我如何只返回结果“名称:”+“名称123”?
【问题讨论】: