Split diary file into multiple files using Python

前端 未结 3 794
余生分开走
余生分开走 2021-01-06 17:19

I keep a diary file of tech notes. Each entry is timestamped like so:

# Monday 02012-05-07 at 01:45:20 PM

This is a sample note

Lorem ipsum dolor sit amet,         


        
3条回答
  •  攒了一身酷
    2021-01-06 17:32

    Here's the general ;-) approach:

    f = open("diaryfile", "r")
    body = []
    for line in f:
        if your_regexp.match(line):
            if body:
                write_one(body)
            body = []
        body.append(line)
    if body:
        write_one(body)
    f.close()
    

    In short, you just keep appending all lines to a list (body). When you find a magical line, you call write_one() to dump what you have so far, and clear the list. The last chunk of the file is a special case, because you're not going to find your magical regexp again. So you again dump what you have after the loop.

    You can make any transformations you like in your write_one() function. For example, sounds like you want to remove the leading "# " from the input timestamp lines. That's fine - just do, e.g.,

    body[0] = body[0][2:]
    

    in write_one. All the lines can be written out in one gulp via, e.g.,

    with open(file_name_extracted_from_body_goes_here, "w") as f:
        f.writelines(body)
    

    You probably want to check that the file doesn't exist first! If it's anything like my diary, the first line of many entries will be "Rotten day." ;-)

提交回复
热议问题