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