我需要检查正在使用的.csv
文件是否以超过1条"\n"
行结尾。如果发现多个空白行,则将其全部删除,但不包含一个空白行。
我的代码是:
import os
from pathlib import Path
def remove_blanks():
dirname = os.path.dirname(os.path.abspath(__file__))
path: Path = Path(os.path.join(dirname, "data.csv"))
with open(path, "r+") as op:
lines = op.readlines()
for line in lines:
if line == "\n":
op.write(line.rstrip("\n"))
.csv
文件类似于['01-01-2019,0,0,0\n', '18-05-2019,33,31,48\n', '\n', '\n', '\n']
,我想要的输出是['01-01-2019,0,0,0\n', '18-05-2019,33,31,48\n', '\n']
,但似乎无法删除任何行。
答案 0 :(得分:1)
最简单的方法是跟踪 空行,然后在编写 non 空行之前写一个。
pre = ""
for line in lines:
if line == "\n":
pre = line
else:
op.write(pre)
op.write(line)
pre = "\n"
op.write(pre)
这会将空行的任何序列减少为单个空行,并在写入非空行或文件末尾之前写入该单行。当pre
为空字符串时,将其写为空操作。
如果要在文件中间保留多个空行,请在找到它们时在pre
中建立空行序列,并在文件末尾仅写一个空行(而不是pre
本身),如果pre
不为空。
pre = ""
for line in lines:
if line == "\n":
pre += line
else:
op.write(pre)
op.write(line)
pre = ""
if pre:
op.write("\n")
答案 1 :(得分:1)
糟糕,切勿重写正在读取的文件:它可能无法正常工作,或充其量只会导致维护噩梦。
如果文件足够小以适合主内存,则代码中的这种轻微更改就足够了:
import os.path
from pathlib import Path
def remove_blanks():
dirname = os.path.dirname(os.path.abspath(__file__))
path: Path = Path(os.path.join(dirname, "data.csv"))
with open(path, "r") as op:
lines = op.readlines() # read lines in memory
with open(path("w") as op: # re-write everything from the beginning
flag = False
for line in lines:
if line == "\n":
if not flag:
op.write(line)
flag = True
else:
op.write(line)
# flag = False # uncomment if you want to keep one blank line
# per group of consecutive lines
答案 2 :(得分:0)
您可以尝试使用Counter()
。
import os
from pathlib import Path
from collections import Counter
def remove_blanks():
dirname = os.path.dirname(os.path.abspath(__file__))
path: Path = Path(os.path.join(dirname, "data.csv"))
with open(path, "r+") as op:
lines = op.readlines()
for line in lines:
count = Counter()
# Add 1 for every time word appears in line
for word in line:
count[word] += 1
# Change the number of newlines to 1
if count['\n'] > 1:
count['\n'] = 1
# Returns list with the number of elements
line = list(count.elements())
答案 3 :(得分:0)
我设法用以下代码解决了这个问题:
import os
from pathlib import Path
def remove_blanks():
dirname = os.path.dirname(os.path.abspath(__file__))
path: Path = Path(os.path.join(dirname, "data.csv"))
with open(path, "r") as op:
lines = op.readlines() # read lines in memory
with open(path, "w") as op: # re-write everything from the beginning
for line in lines:
if line != "\n":
op.write(line)
else:
continue
它可以删除多余的每一行,无论它在文件中的什么位置。
感谢所有试图帮助我的人!