我想在字符串id
和name
中找到一个特定的两个单词,我想使用正则表达式,但我不能格式化。你知道吗
在我的档案中:
<p>Any text, bla bla lorem ipsum, bla bla</p>
<p>test = {"player":{"id":"123123","name":"f_teste"};
以下是我的进展:
import re
def main():
padrao = r'"id"\w+'
caminho = 'D:\index.txt'
arquivo = open(caminho,'r')
texto = arquivo.readlines()[1].split('{')
textoEncontrado = texto[2].split(',')
print textoEncontrado[0]
print textoEncontrado[1]
arquivo.close()
if __name__ == '__main__':
main()
结果:
"id":"123123"
"name":"f_teste"};
我想要的是:
id: 123123
name = f_teste
当我尝试使用RE只获取字符串id
时,得到:
padrao = r'^id$'
(...)
result = re.findall(padrao,textoEncontrado[0])
print result
(...)
结果是[]
对不起,英语不好。
谢谢大家。:)
如果您的输入是包含json文本的有效html:
要安装
bs4
,请运行:pip install beautifulsoup4
。你知道吗正则表达式解决方案如下所示:
相关问题 更多 >
编程相关推荐