如何更改文本文件中的特定行并在写入输出文件时强制执行此更改

How to change specific lines in a text file and enforce this change when writing output file

我在对文本文件中的特定文件实施更改时遇到问题。我遍历了这些行并确定了以特定字符 (N2) 开头的行。

我试图换行一个段落,因此它只允许每行 100 个字符作为来自在线资源的此输出的摘要,文件中包含大量摘要,所有摘要都以字符串前缀 N2 开头。

信息在文本文件中显示为单独的行,ForEoin.txt:

<!-- language: lang-none -->
TY  - JOUR 
ID  - 31513460
T1  - Systematic Review: Clinical Metabolomics to Forecast Outcomes in Liver Transplantation Surgery.
A1  - Attard, Joseph A
A1  - Dunn, Warwick B
A1  - Mergental, Hynek
A1  - Mirza, Darius F
A1  - Afford, Simon C
A1  - Perera, M Thamara P R
Y1  - 2019//
N2  - Liver transplantation is an effective intervention for end-stage liver disease, fulminant hepatic failure, and early hepatocellular carcinoma. Yet, there is marked patient-to-patient variation in liver transplantation outcomes. This calls for novel diagnostics to enable rational deployment of donor livers. Metabolomics is a postgenomic high-throughput systems biology approach to diagnostic innovation in clinical medicine. We report here an original systematic review of the metabolomic studies that have identified putative biomarkers in the context of liver transplantation. Eighteen studies met the inclusion criteria that involved sampling of blood (n = 4), dialysate fluid (n = 4), bile (n = 5), and liver tissue (n = 5). Metabolites of amino acid and nitrogen metabolism, anaerobic glycolysis, lipid breakdown products, and bile acid metabolism were significantly different in transplanted livers with and without graft dysfunction. However, criteria for defining the graft dysfunction varied across studies. This systematic review demonstrates that metabolomics can be deployed in identification of metabolic indicators of graft dysfunction with a view to implicated molecular mechanisms. We conclude the article with a horizon scanning of metabolomics technology in liver transplantation and its future prospects and challenges in research and clinical practice.
KW  - *Biomarkers
KW  - Genotype

到目前为止,我已经遍历了文件的行并调用了 textwrap 模块来为我包装它,但是我无法在输出文件中用这个新包装的行来覆盖现有的行。

#!/usr/bin/env python

import textwrap

filename_org = 'ForEoin.txt'
filename_new = 'Eoin_Shortline_v2'


with open(filename_org, 'r') as rf:
    with open(filename_new, 'w') as wf:
        for line in rf:
            if line.startswith("N2"):
                wrapper = textwrap.TextWrapper(width=100)
                new_line = wrapper.fill(text=line)

                wf.write(new_line)

您是否只需要一个 else 语句,在不以 N2 开头的情况下不改变地写入该行?

with open(filename_org, 'r') as rf:
    with open(filename_new, 'w') as wf:
        for line in rf:
            if line.startswith("N2"):
                wrapper = textwrap.TextWrapper(width=100)
                new_line = wrapper.fill(text=line)
                wf.write(new_line)
            else:
                wf.write(line)
import textwrap

filename_org = 'ForEoin.txt'
filename_new = 'Eoin_Shortline_v2'


with open(filename_org, 'r') as rf:
    with open(filename_new, 'w') as wf:
        for line in rf:
            if line.startswith("N2"):
                wrapper = textwrap.TextWrapper(width=100)
                new_line = wrapper.fill(text=line)
                wf.write(new_line)
            else:
                wf.write(line)

如果该行以 "N2" 开头,则进行包装,然后在其他部分将其写入文件,您还必须将其正确写入文件。