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

后端 未结 7 1918
[愿得一人]
[愿得一人] 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 00:59

    You can try something like this :

    import re
    regex = re.compile("f\(\s*([^,]+)\s*,\s*([^,]+)\s*\)")
    with open("my_file.txt") as f:
        for line in f:
            result = regex.search(line)
    

提交回复
热议问题