
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.