如何从 Python 中的文件中删除“\n”行?
How can I delete "\n" lines from a file in Python?
我需要检查我正在使用的 .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']
,但它似乎无法删除任何行。
最简单的方法是,如果您看到 一个 空行,那么在写一个 non[=24= 之前写一个]-空行。
pre = ""
for line in lines:
if line == "\n":
pre = line
else:
op.write(pre)
op.write(line)
pre = "\n"
op.write(pre)
这会将任何空行序列减少为单个空行,并在写入 non-empty 行或文件末尾之前写入该行。当pre
为空串时,写成no-op.
如果要在文件中间保留多个空行,在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")
您可以尝试使用 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())
哎呀,永远不要重写您正在阅读的文件:它可能无法工作,或者充其量只会导致维护噩梦。
如果文件足够小,可以放入主内存,那么对您的代码稍作改动就足够了:
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
我用这段代码设法解决了这个问题:
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
它可以删除每一个多余的新行,无论它在文件中的什么位置。
感谢所有帮助过我的人!
我需要检查我正在使用的 .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']
,但它似乎无法删除任何行。
最简单的方法是,如果您看到 一个 空行,那么在写一个 non[=24= 之前写一个]-空行。
pre = ""
for line in lines:
if line == "\n":
pre = line
else:
op.write(pre)
op.write(line)
pre = "\n"
op.write(pre)
这会将任何空行序列减少为单个空行,并在写入 non-empty 行或文件末尾之前写入该行。当pre
为空串时,写成no-op.
如果要在文件中间保留多个空行,在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")
您可以尝试使用 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())
哎呀,永远不要重写您正在阅读的文件:它可能无法工作,或者充其量只会导致维护噩梦。
如果文件足够小,可以放入主内存,那么对您的代码稍作改动就足够了:
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
我用这段代码设法解决了这个问题:
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
它可以删除每一个多余的新行,无论它在文件中的什么位置。
感谢所有帮助过我的人!