gpt4 book ai didi

python - 一个用于从 python 中的文件中删除空行的衬垫?

转载 作者:太空狗 更新时间:2023-10-30 01:41:44 24 4
gpt4 key购买 nike

我正在寻找一种可以从 python 文件中删除所有空行的衬垫。python 等价于 --> grep -v '^$' file_name > file_name

最佳答案

lines = [i for i in open(file_path) if i[:-1]]

如果需要写入另一个文件,您可以使用 file_object.writelines(lines) 打开文件进行写入。

关于python - 一个用于从 python 中的文件中删除空行的衬垫?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6745854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com