Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在读取文件期间,用单个换行符替换多个换行符_Python_Regex_File - Fatal编程技术网

Python 在读取文件期间,用单个换行符替换多个换行符

Python 在读取文件期间,用单个换行符替换多个换行符,python,regex,file,Python,Regex,File,我有下一个代码,它读取多个文件,解析获得的行并打印结果: import os import re files=[] pars=[] for i in os.listdir('path_to_dir_with_files'): files.append(i) for f in files: with open('path_to_dir_with_files'+str(f), 'r') as a: pars.append(re.sub('someword=|\,.

我有下一个代码,它读取多个文件,解析获得的行并打印结果:

import os
import re

files=[]
pars=[]

for i in os.listdir('path_to_dir_with_files'):
    files.append(i)

for f in files:
    with open('path_to_dir_with_files'+str(f), 'r') as a:
       pars.append(re.sub('someword=|\,.*|\#.*','',a.read()))

for k in pars:
   print k
但我对输出中的多个新行有问题:

test1


test2
 test1
 test2
我希望获得下一个结果,而不是输出中没有空行:

test1


test2
 test1
 test2
等等

我尝试过使用regexp:

pars.append(re.sub('someword=|\,.*|\#.*|^\n$','',a.read()))

但它不起作用。我还尝试使用strip()和rstrip(),包括replace。它也不起作用。

在不改变代码的情况下,一个简单的方法就是在打印前检查行是否为空,例如:

import os
import re

files=[]
pars=[]

for i in os.listdir('path_to_dir_with_files'):
    files.append(i)

for f in files:
    with open('path_to_dir_with_files'+str(f), 'r') as a:
        pars.append(re.sub('someword=|\,.*|\#.*','',a.read()))

for k in pars:
    if not k.strip() == "":
        print k
***编辑 由于PAR中的每个元素实际上是文件的全部内容(而不仅仅是一行),因此您需要通过替换任何双端行,这是使用re最容易做到的

import os
import re

files=[]
pars=[]

for i in os.listdir('path_to_dir_with_files'):
    files.append(i)

for f in files:
    with open('path_to_dir_with_files'+str(f), 'r') as a:
        pars.append(re.sub('someword=|\,.*|\#.*','',a.read()))

for k in pars:
    k = re.sub(r"\n+", "\n", k)
    if not k.strip() == "":
        print k

请注意,这并不考虑文件以换行结束而下一行以换行开始的情况-如果您担心这种情况,您需要添加额外的逻辑来处理它,或者更改读取数据的方式,您可以使用第二个正则表达式将多个新行替换为一个新行并使用脱掉最后一条新线

import os
import re

files=[]
pars=[]

for i in os.listdir('path_to_dir_with_files'):
    files.append(i)

for f in files:
    with open('path_to_dir_with_files/'+str(f), 'r') as a:
        word = re.sub(r'someword=|\,.*|\#.*','', a.read())
        word = re.sub(r'\n+', '\n', word).strip()
        pars.append(word)

for k in pars:
   print k

我只想指出:正则表达式并不是最好的处理方法。在Python str中用一行替换两行空行非常简单,无需重新:

entire_file = "whatever\nmay\n\nhappen"
entire_file = entire_file.replace("\n\n", "\n")

瞧!比re快得多,而且(在我看来)更容易阅读。

或者如果k.strip()这也应该在添加到
pars
时进行,而不是在迭代
pars
时进行。不幸的是,它没有给出适当的结果。如果不是k.strip()='',我仍然获得多个空行。如果只显示列表而不进行迭代,我会得到:test1[]\n\n\n test2\n test5\ntest7[]\ntest[*]\n等等……哦,我明白了,因为您只是将整行读取到PAR中的每个项目中,所以它不是逐行打印。我编辑了我的答案,它只是使用正则表达式来处理和消除任何重复的\n,非常感谢您!如果文件包含两个以上连续的“\n”例如“which\n可能\n\n\n appen”,则此操作无效。这是真的,但仍然可以使用循环:
,而“\n\n”在text:text=text.replace(“\n\n”,“\n”)
此形式的“省略”是脆弱的,需要根据所需运行的长度进行自适应。例如,需要在“段落”之间添加两个换行符需要三次
。替换(“\n\n\n”,“\n\n”)
调用。迭代重建意味着每次迭代都要复制整个字符串。正则表达式可以更容易地将实际测量的重复字符运行与对运行长度的显式控制结合起来:
\n{min,max}
,并在O(1)时间内执行这样的操作,而不会产生过多的内存重复。您能按行而不是按文件执行吗?比如f中的行:你能解释一下re.sub是做什么的吗?逗号和散列被转义,我不理解someword=。示例中没有=。当然可以按行操作,但在本例中,
f
是文件名,而不是内容
re.sub
将与第一个参数匹配的内容替换为第二个参数中的内容。检查文档并尝试一下。