【发布时间】:2013-03-31 00:13:27
【问题描述】:
我希望我的 python 程序在文本文件中搜索字符串的特定部分。 例如,我的文本文件如下所示:
VERSION_1_0001
VERSION_2_0012
VERSION_3_0391
这些只是示例。我希望我的 python 程序查找“VERSION_2_”,但让它在另一个文本文件中打印出 0012。这可能吗?
到目前为止,我只有这个:
with open('versions.txt', 'r') as verFile:
for line in verFile:
if 'VERSION_2_' in line:
??? (I don't know what would go here so I can get the portion attached to the string I'm finding)
提前感谢您的帮助!
【问题讨论】:
-
你真的要检查
'VERSION_2_' in line而不是line.startswith('VERSION_2_')吗? -
我宁愿签入行,因为在某些文本文件中,实际 VERSION 之前有各种字符。所以它可能并不总是以 VERSION 开头。
-
好的,您肯定已经了解您的数据,深思熟虑,并做出了正确的决定。酷。