我有一个包含值为2000,00的文件。删除文件中的空格和空行使用Python
但它包含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()
没有的Python:'猫input.txt中| egrep -v'^ \ s * $'> output.txt' – eumiro
这会留下数字后的空格。也许增加一个'tr':'cat input.txt | egrep -v'^ \ s * $'| tr -d''> output.txt' –