Skip to main content
 首页 » 编程设计

Python在两个引号之间查找以特定单词开头的行

2024年12月31日7三少

我有一个看起来像这样的文本文件:

This is a text file  
game:  
" 
a 
b 
" 
symbol : "o" 

我想使用“game:”旁边和引号之间的行。 我尝试使用 re 例如:

contents = file.read() 
re.findall((?<=game)"([^"]*)",contents) 

但这行不通... 有人能帮助我吗? 谢谢!

请您参考如下方法:

您需要考虑 game: ":

之间和之后的换行符和空格
re.findall(r"game: \n\"\n([^\"]*)",contents)