使用正则表达式获取两个特定词之间的数据:
(Python代码)
当你想取得数据前面有约束条件,后面没有约束条件(也就是每行的最后一段数据时),最好使用\n作为后面的约束条件。如下例子所示:
>>>pattern = re.compile(r'(?<=}]}},).*?(?=\n)')
>>>result_str = ''.join(pattern.findall(line))
>>>file.write(result_str + "\n")
- re.compile()函数
用来生成一个满足需求的正则表达式(每行的匹配条件) - .findall()函数
获取满足条件的数据(获取line中满足条件“}]}},”之后的所有数据。后面必须加约束条件“\n”) - ’’.join()函数
list类型的数据转换成str类型数据(.write()函数只能写入str类型数据) - file.write()函数
给文件名为file的文件中写数据
相关参考文档:
https://www.runoob.com/python/python-reg-expressions.html
https://blog.csdn.net/xufei512/article/details/82388881
("Doing the things you want to do." -- 《The Second Best Exotic Marigold Hotel》
)