- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Remove Newline Characters from a text File?
In this article, we will show you how to remove the newline character(\n) from a given text file using python.
Assume we have taken a text file with the name TextFile.txt consisting of some random text. We will remove the newline character(\n) from a given text file.
TextFile.txt
Good Morning TutorialsPoint This is TutorialsPoint sample File Consisting of Specific source codes in Python,Seaborn,Scala Summary and Explanation Welcome TutorialsPoint Learn with a joy
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
Create a variable to store the path of the text file.
Use the open() function(opens a file and returns a file object as a result) to open the text file in read-only mode by passing the file name, and mode as arguments to it (Here “r” represents read-only mode).
with open(inputFile, 'r') as filedata:
Using the readlines() function (returns a list with each line in the file represented as a list item. To limit the number of lines returned, use the hint argument. No more lines are returned if the total amount of bytes returned exceeds the specified number) to obtain the list of lines of a given input text file with a new line character (\n) at the end.
file.readlines(hint)
Use the rstrip() function(removes any trailing characters i.e, characters present at the end of a string. The default trailing character to remove is space) and list comprehension(here we are iterating in each line of the list using the for loop), to remove the newline character(\n) from the above list of lines of a text file and print them.
list comprehension: When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.
Close the input file with the close() function(used to close an opened file).
Example
The following program checks line by line if the given word is found in a line from a text file and prints the line if the word is found −
# input text file inputFile = "ExampleTextFile.txt" # Opening the given file in read-only mode with open(inputFile, 'r') as filedata: # Reading the file lines using readlines() linesList= filedata.readlines() # Removing the new line character(\n) from the list of lines print([k.rstrip('\n') for k in linesList]) # Closing the input file filedata.close()
Output
On executing, the above program will generate the following output −
['Good Morning TutorialsPoint', 'This is TutorialsPoint sample File', 'Consisting of Specific', 'source codes in Python, Seaborn,Scala', 'Summary and Explanation', 'Welcome TutorialsPoint', 'Learn with a joy']
We gave our program a text file containing some random content and then opened it in reading mode. The readlines() function was then used to retrieve a list of all the lines in the file. Using the list comprehension, we went over each line of the file and deleted the newline character with the rstrip() method. Finally, we closed the file by printing the updated lines without the new line characters.
So, from this article, we learned how to open a file and read lines from it, which can be used to execute operations such as finding the number of words in a line, the length of a line, and so on. We also learned how to use list comprehension for precise and simple code, as well as how to delete newline characters from every line of the file. This method may also be used to remove any specific letters/words from the file's lines.
- Related Articles
- Remove newline, space and tab characters from a string in Java
- How to read a number of characters from a text file using Python?
- How to Remove Characters from a String in Arduino?
- How to remove certain characters from a string in C++?
- How to remove specific characters from a string in Python?
- How to remove non-ASCII characters from strings
- How to create a Python dictionary from text file?
- How to remove text from a label in Python?
- How to remove special characters from a database field in MySQL?
- How to Remove Empty Lines from a File on ubuntu
- How to remove a committed file from the Git repository?
- How to read a text file from resources in Kotlin?
- Load a text file and find number of characters in the file - JavaScript
- How to split on successions of newline characters using Python regular expression?
- How to count the number of characters (including spaces) in a text file using Java?
