Read multiple block of file between start and stop flags

前端 未结 4 1183
轮回少年
轮回少年 2020-12-06 23:53

I am trying to read sections of a file into numpy arrays that have similar start and stop flags for the different sections of the file. At the moment I have found a method

4条回答
  •  一个人的身影
    2020-12-07 00:27

    Let's say this is your file to read:

    **starting** blabla blabla **starting** bleble bleble **starting** bumbum bumbum

    This is code of the program:

    file = open("testfile.txt", "r")
    data = file.read()
    file.close
    data = data.split("**starting**")
    print(data)
    

    And this is output:

    ['', '\nblabla\nblabla\n', '\nbleble\nbleble\n', '\nbumbum\nbumbum']

    Later you can del empty element, or do other operation in your data. split function is buildin for string objects and can get more complicated strings as arguments.

提交回复
热议问题