我有一个文本文件,看起来像这样。
test=HelloWorld
test=
test2=blabla
我怎么能只打印后面的内容 test=
? 谢谢!我有一个文本文件是这样的:test=HelloWorld test= test2=blabla 如何才能只打印test=后面的内容?
这能解决你的问题吗?
txt = """
test=HelloWorld
test=
test2=blabla"""
for line in txt.split('\n'):
split = line.split('=')
if split[0] == 'test':
print(split[1])
字符串的例子,当然,你需要正确加载你的文件。
你可以用split()拆分特定字符的字符串,返回一个列表。
你需要使用regex来查找text=后面的内容,比如:".*=(.*)"。 ".*=(.*)
然后抓住match1中的内容。
参见这里了解如何在 Python 中使用 regex。