我有一個包含值爲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' –