Search code examples
pythonpython-3.xpandastextfile-handling

Separating all passages from a text file and saving individual text file of each separated passage using python


Summary of the problem: I have a text file which contains 100 passages. I need to separate out all those 100 passages and save them individually in 100 text files.

Pattern of passages in input text file:

25763772|t|DCTN4 as a modifier of chronic Pseudomonas aeruginosa infection in cystic fibrosis
25763772|a|Pseudomonas aeruginosa (Pa) infection in cystic fibrosis (CF) patients is present
25763772    0   5   DCTN4   T116,T123   C4308010
25763772    23  63  chronic Pseudomonas aeruginosa infection    T047    C0854135
25763772    67  82  cystic fibrosis T047    C0010674

25847295|t|Nonylphenol diethoxylate inhibits apoptosis induced in PC12 cells
25847295|a|Nonylphenol and short-chain nonylphenol ethoxylates such as NP2 EO are digested
25847295    0   24  Nonylphenol diethoxylate    T131    C1254354
25847295    25  33  inhibits    T052    C3463820

Likewise there are 100 passages of variable lengths present in that single text file.

I'm trying a code like this which is not showing any error but not able to extract and save even a single passage individually. Please suggest any kind of help or solution on this. Thanks in advance.

Code:

with open('corpus_pubtator1.txt', 'r') as contents, open('tested23.txt', 'w') as file:
    contents = contents.read()
    lines = contents.split('\n')
    for index, line in enumerate(lines):
        if index != len(lines) - 1:
            file.write(line + '.\n')
        else:
            pass

Solution

  • Try this :

    lines = []
    with open("corpus_pubtator1.txt", "r") as rf:
        lines = rf.readlines()
    lines = [i if i else i.strip() for i in lines]
    passages = []
    passage_cache = []
    for i, line in enumerate(lines):
        if i == len(lines) - 1:
            passages.append(passage_cache)
        if line.strip():
            passage_cache.append(line)
        else:
            passages.append(passage_cache)
            passage_cache = [line]
    for i, passage in enumerate(passages):
        with open(f"tested{i}.txt", 'w') as wf:
            for line in passage:
                wf.write(line)
    

    It would open the first input file, read all the lines and differentiate the passages wrt a vacant line in between and for each passages it would create a separate text file and write lines inside it.