It is quite a common requirement for users to remove certain characters from their text files while displaying. This is done to assure that only displayable characters are displayed or data should be displayed in a specific structure. This article will teach you how to read a text file into a string variable and strip newlines using Python.
For demonstration, we would be using the following file:
Python Read File into String Methods
- Using rstrip() function
- Using replace() function
- Using split() function
- Using splitlines() function
- Using strip function
Python Read file into String using rstrip
We can use rstrip in a List comprehension to read a text file into a string variable and strip newlines in Python. Here is an example of using the rstrip function in Python to read files into string variables and strip newlines.
Python3
with open ( 'text.txt' , 'r' ) as file : text = " " .join(line.rstrip() for line in file ) print (text) |
Output
This is fame not clout You don't even know what Rollex Links
Python Read file into String using Replace() Function
The task could be performed using the replace function, a default function in all Python distributions. Where old is the string to be replaced and new is the string that will replace it. Firstly the path to the file is defined. Then the file’s contents are read and stored in the variable named data. All the occurrences of the newline character in the variable data are replaced with the empty string (nothing). In the end, the data after stripping the newlines are displayed.
Python3
# Variable contains the path to the file path = r "C:\Users\priviet.txt" # The file is read and its data is stored data = open (path, 'r' ).read() # Replacing all occurrences of newline in data with '' data = data.replace( '\n' , '') # Displaying the resultant data print (data) |
Output
This is fame not clout You don't even know what Rollex Links
Python Read file into String using split function
The task could also be performed using the split function, a default function in all Python distributions. The function takes in an argument (optional) a character (or string) and splits the string based on the occurrence of that character in it. Firstly the file is read and stored in a variable as before. Then the variable is passed through the split function, which splits the string and creates a list based on the occurrences of the passed argument. In this case, it was the newline character. Thus after this process, we ended up with a list containing substrings of the original strings.
Python3
# Variable contains the path to the file path = r "C:\Users\priviet.txt" # The file is read and its data is stored data = open (path, 'r' ).read() # We splitted the string based on the # occurrence of newline character # A new list is created where newline # character is not present in any element data = data.split( "\n" ) # Joined all the elements of the list to # form a single string data = "".join(data) # Displaying the resultant data print (data) |
Output
This is fame not clout You don't even know what Rollex Links
Python Read file into String using Splitlines function
The Splitlines is a function that splits/breaks a string based on the occurrence of escape sequences. The function converts the string to a list upon stripping the control characters. Hence, all the list elements must be iterated to get the final string. This method is almost identical to the split method described earlier. The only difference is that the split lines function does not require any argument and is made to work only with line boundaries. Hence, the splitting of data would be made on any occurrence of a line boundary.
Python3
# Variable contains the path to the file path = r "C:\Users\priviet.txt" # The file is read and its data is stored data = open (path, 'r' ).read() # We splitted the string based on the occurrence of line boundaries # A new list is created where newline character is not present in any element data = data.splitlines() # Joined all the elements of the list to form a single string data = "".join(data) # Displaying the resultant data print (data) |
Output
This is fame not clout You don't even know what Rollex Links
Python Read file into String using strip function
The split function could also remove the newline character from a text file. The function optionally takes in argument a separator and strips the leading and trailing separator in the string. Hence, if each line of the text file is passed as an argument and the function is called on the contents of each line, all the newline characters would be stripped from it. Firstly the contents of the text file are read per line, and each line is stored in a list variable.
Python3
# Variable contains the path to the file path = r "C:\Users\priviet.txt" # Each line of the file is read, and # stored in a list variable data = open (path, 'r' ).readlines() # The loop iterates over each line of the file for x in data: # Stripping the newline character from each line # and displaying it sequentially. print (x.strip( '\n' ), end = "") |
Output
This is fame not clout You don't even know what Rollex Links