How to use regexp on file, line by line, in Python

后端 未结 7 1941
[愿得一人]
[愿得一人] 2020-12-30 00:24

Here is my regexp: f\\(\\s*([^,]+)\\s*,\\s*([^,]+)\\s*\\)

I\'d have to apply this on a file, line by line. The line by line is OK, simple reading from

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-30 01:14

    The following expression returns a list; every entry of that list contains all matches of your regexp in the respective line.

    >>> import re
    >>> [re.findall(r'f\(\s*([^,]+)\s*,\s*([^,]+)\s*\)',line) 
                for line in open('file.txt')]
    

提交回复
热议问题