简体   繁体   English

如何根据txt文件中的某些条件删除空行?

[英]How to remove empty lines depending on certain conditions in a txt file?

input_file_1.txt




Python is a high-level, general-purpose programming language


This book is an introduction to programming in Oracle’s Java™ programming language
An object is composed of fields and methods

A shell is a command-line interpreter and typical operations performed by shell scripts include file manipulation, program execution, and printing text.




I also have other files called input_file_2.txt and input_file_3.txt , and they both have the same content as input_file_1.txt .我还有其他名为input_file_2.txtinput_file_3.txt的文件,它们的内容都与input_file_1.txt相同。

For the file input_file_1.txt I need to read it and remove all the possible empty lines that are at the beginning, except one.对于文件input_file_1.txt ,我需要阅读它并删除开头所有可能的空行,除了一个。

input_file_1.txt


Python is a high-level, general-purpose programming language


This book is an introduction to programming in Oracle’s Java™ programming language
An object is composed of fields and methods

A shell is a command-line interpreter and typical operations performed by shell scripts include file manipulation, program execution, and printing text.




For the file input_file_2.txt I need to read it and remove all the possible empty lines that are at the end, except one对于文件input_file_2.txt我需要阅读它并删除最后所有可能的空行,除了一个

input_file_2.txt




Python is a high-level, general-purpose programming language


This book is an introduction to programming in Oracle’s Java™ programming language
An object is composed of fields and methods

A shell is a command-line interpreter and typical operations performed by shell scripts include file manipulation, program execution, and printing text.

And for the input_file_3.txt file I need to read it and delete those empty lines that are in the middle of 2 non-empty lines.对于input_file_3.txt文件,我需要阅读它并删除那些位于 2 个非空行中间的空行。

input_file_3.txt




Python is a high-level, general-purpose programming language
This book is an introduction to programming in Oracle’s Java™ programming language
An object is composed of fields and methods
A shell is a command-line interpreter and typical operations performed by shell scripts include file manipulation, program execution, and printing text.




How should I correctly read each of these 3 files that are initially the same but then I will modify each one so that they meet the conditions of each of these 3 cases?我应该如何正确阅读最初相同的这三个文件中的每一个,然后我将修改每个文件以使它们满足这三个案例中的每一个的条件?

You can look at any doccumentation online.您可以在线查看任何文档。 There are many websites, such as this one https://datagy.io/python-read-text-file/ which I just googled, that will help you achieve what you're trying to do.有很多网站,比如我刚刚用谷歌搜索的这个https://datagy.io/python-read-text-file/ ,这将帮助您实现您想要做的事情。

You can also just check whether certain lines are empty or only contain '\n' and skip over those lines.您也可以只检查某些行是否为空或仅包含 '\n' 并跳过这些行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM