将包含某些字符串的行中的单空格替换双倍空格



我有一个包含行和列的大型文本文件。在文件中的所有字符串/数据之间,有一个双倍空格。但是,为了使我的特定代码正常工作,我需要双空格仅在某些行中成为单个空格。这些行都以相同的字符串开头。

我试过:

with open(outfile) as f3, open(outfile2,'w') as f4:
    for line in f3:
         line = line.strip()
         if "SAMPLE" in line:
             " ".join(line.split())
         if 'xyz' not in line and len(line) >=46:
             f4.write(line+'n')  

我试过:

import re
with open(outfile) as f3, open(outfile2,'w') as f4:
    for line in f3:
         if "SAMPLE" in line:
             re.sub("ss+" , " ", line)
         if 'xyz' not in line and len(line) >=46:
             f4.write(line)  

两者都不行。第二个 if 语句删除一些我不想要的行,这样就不会消失(这按预期工作(。但是,文本文件中所有数据之间的双倍间距仍然存在。如何使文件中包含"SAMPLE"的行用单倍行距替换行中单词之间的双空格?

你的问题是字符串的可变性," ".join(line.split())创建一个新字符串,这很可能是你需要的,但你应该把它赋回line变量。

if "SAMPLE" in line:
    line = " ".join(line.split())

后期编辑:
第二个if有点"奇怪"...预期的结果是什么?

if not line or (':' and len(line) >=46):
    f4.write(line) 

尤其是第二部分... ':'评估总是True,似乎无用可能是错别字或缺少什么。仅当line为空或 None(计算结果为 False (或行长为 >= 46 时,才会写入文件。

代码应如下所示:

with open(outfile) as f3, open(outfile2,'w') as f4:
    for line in f3:
         line = line.strip()
         if "SAMPLE" in line:
             # we clean eventual double/multi-space if the line contains "SAMPLE"
             line = " ".join(line.split()) 
         if 'xyz' not in line and len(line) >=46:
             # write to the second file only the lines that
             # don't contain 'xyz' and have the length of the line => 46 
             f4.write(line+'n')  

试试这个:

s = " ".join(your_string.split())

最新更新