我对 Python 没有那么丰富的经验,因此我请求帮助我改进我的代码。
我正在尝试解析“名称”字段下的“史蒂夫”:
xxxx xxxx xxxx Name
zzzz zzzz zzzz Steve
我的代码是这样的:
for line in myfile.readlines():
[..]
if re.search(r'Name =', line):
print("Destination = ")
samples+=line[15:19]
nextline = "y"
if nextline == 'y':
samples+=line[15:19]
最终我将打印所有内容:
[..]
for s in samples:
myfile2.write(s)
它确实有效,但我不敢相信没有更聪明的方法可以做到这一点(比如在满足条件后访问以下行......)。
这是我需要解析的文件的示例。但结构可能会有所不同,例如
#This is another example
Name =
Steve
任何帮助表示赞赏。
千巷猫影
繁星coding
守着一只汪
相关分类