Python efficient way to check if very large string contains a substring

后端 未结 8 1780
無奈伤痛
無奈伤痛 2021-01-02 05:04

Python is not my best language, and so I\'m not all that good at finding the most efficient solutions to some of my problems. I have a very large string (coming from a 30 MB

8条回答
  •  耶瑟儿~
    2021-01-02 06:08

    If you just want to check if that substring exists,

    for line in open("file"):
        if substring in line:
             print "exists"
             sys.exit() # or break to do some other stuff
    

提交回复
热议问题