如何改变文本文件中的特定行数,并在编写输出文件时强制执行这一改变。

问题描述 投票:0回答:1

我在对一个文本文件中的特定文件进行修改时遇到了一个问题。我已经循环过这些行,并确定了那些以特定字符(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)
python io overwrite
1个回答
0
投票

你是否只需要一个 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)

0
投票
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 "开头,做封装,然后写到文件中,在else部分也要把它右移到文件中。

© www.soinside.com 2019 - 2024. All rights reserved.