The program is as follows:
for line in file.readlines(): line=line.strip('\n')
Use the strip() function to remove the \n at the end of each line
strip() function prototype
Statement: str is a string, chars is the character sequence to be deleted
str.strip(chars): delete the characters at the beginning and end of the s string, located in the chars deletion sequence
str.lstrip(chars): Delete the characters at the beginning of the s string, located in the chars deletion sequence
str.rstrip(chars): Delete the characters at the end of the s string, located in the chars deletion sequence
Note:
When chars is empty, whitespace characters (including '\n', '\r', '\t', ' ') are deleted by default.
More For Python related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of How to remove the newline character '\n' at the end of each line in python. For more information, please follow other related articles on the PHP Chinese website!