Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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_File - Fatal编程技术网

使用Python从文件中删除空格和空行

使用Python从文件中删除空格和空行,python,file,Python,File,我有一个包含值2000,00的文件 但它包含2000,00之后的空格和空行 我想删除所有的空格和空行,如果有人能给出一些想法,我尝试了很多方法,但没有成功 有一种方法我很累,如下所示 # Read lines as a list fh = open("transfer-out/" + file, "r") lines = fh.readlines() fh.close() # Weed out blank lines with filter lines = filter(lambda x

我有一个包含值2000,00的文件

但它包含2000,00之后的空格和空行

我想删除所有的空格和空行,如果有人能给出一些想法,我尝试了很多方法,但没有成功

有一种方法我很累,如下所示

    # Read lines as a list
fh = open("transfer-out/" + file, "r")
lines = fh.readlines()
fh.close()
# Weed out blank lines with filter
lines = filter(lambda x: not x.isspace(), lines)
# Write "transfer-out/"+file+".txt", "w"
fh = open("transfer-out/"+file, "w")
#fh.write("".join(lines))
# should also work instead of joining the list:
fh.writelines(lines)
fh.close()
strip()
删除前导和尾随空格字符

with open("transfer-out/" + file, "r") as f:
    for line in f:
        cleanedLine = line.strip()
        if cleanedLine: # is not empty
            print(cleanedLine)

然后,您可以将脚本重定向到文件
python clean_number.py>file.txt
,例如。

这应该可以按照您的意愿工作:

file(filename_out, "w").write(file(filename_in).read().strip())
编辑:尽管前面的代码在Python2.x中可用,但在Python3中不可用(请参见@gnibbler comment),对于这两个版本,请使用以下代码:

open(filename_out, "w").write(open(filename_in).read().strip())

另一个列表理解:

clean_lines = []
with open("transfer-out/" + file, "r") as f:
    lines = f.readlines()
    clean_lines = [l.strip() for l in lines if l.strip()]

with open("transfer-out/"+file, "w") as f:
    f.writelines('\n'.join(clean_lines))

更改“lines”行以使用以下生成器,它应该可以完成此任务

lines = (line.strip() for line in fh.readlines() if len(line.strip()))
功能一:)

用法:

$ yes "2000,00  " | head -n 100000 > data.txt
$ python -c "print '\n'*100000" >> data.txt
$ wc -l data.txt 
200001 data.txt
$ python filt.py > output.txt
$ wc -l output.txt 
100000 output.txt

如果不使用Python:
cat input.txt | egrep-v'^\s*$'>output.txt
这将在数字后面留下空格。可能添加一个
tr
cat input.txt ^\s*$'\tr-d'>output.txt
你应该添加一个连接:
f.writelines('\n'.join(clean_line))
。我想你的意思是
打开
而不是
文件
在这里。我是说文件。我更喜欢文件,但根据doc()的规定,open是可前置的:-(@BurhanKhalid,
file
open
的一个不推荐使用的同义词。它存在于Python2中,因为
open
是子类化
file
或与
isinstance
一起使用的一个奇怪名称。它在Python2中不再存在Python3@Jiri什么是filename_out和filename_in?@gnibler哦,是的,我大部分时间都在使用2.x,我我还没有意识到这一点!我认为是时候使用open而不是file了。如果排除那些重复的
strip()
调用,那么您就成功了。:-)这只是包含名为
file
的文件的目录名。很抱歉,我读不懂你的代码!我必须从文件夹中访问我的文件,并删除其中的空格和空行!还有一件事是,如果len(line.strip())可以替换为
if line.strip()
$ yes "2000,00  " | head -n 100000 > data.txt
$ python -c "print '\n'*100000" >> data.txt
$ wc -l data.txt 
200001 data.txt
$ python filt.py > output.txt
$ wc -l output.txt 
100000 output.txt