Splitting large text file by a delimiter in Python

前端 未结 2 381
栀梦
栀梦 2020-12-03 06:07

I imaging this is going to be a simple task but I can\'t find what I am looking for exactly in previous StackOverflow questions to here goes...

I have large text fil

2条回答
  •  死守一世寂寞
    2020-12-03 06:41

    If every entry block starts with a colon, you can just split by that:

    with  open('entries.txt') as fp:
        contents = fp.read()
        for entry in contents.split(':'):
            # do something with entry  
    

提交回复
热议问题