how do i remove end of line in python? [duplicate]

  • Last Update :
  • Techknowledgy :

The following code uses the strip() function to remove a newline character from a string in Python.

str1 = "\n Starbucks has the best coffee \n"
newstr = str1.strip()
print(newstr)

Output:

Starbucks has the best coffee
3._
str1 = "\n Starbucks has the best coffee \n"
newstr = str1.rstrip()
print(newstr)
6._
New list: ['Starbucks', 'has the best', 'coffee ']

The following code uses the re.sub() function to remove a newline character from a string in Python.

#import the regex library
import re

list1 = ["Starbucks\n", "has the \nbest", "coffee\n\n "]

rez = []
for sub in list1:
   rez.append(sub.replace("\n", ""))

print("New List : " + str(rez))

Suggestion : 2

Python program to remove special characters from all files in a folder,In this tutorial, we will learn how to remove the duplicate lines from a text file using python. The program will first read the lines of an input text file and write the lines to one output text file.,After everything is completed, the output file will contain all the contents of the input file without any duplicate lines.,Python program to count the total number of lines in a file

First Line
Second Line
First Line
First Line
First Line
First Line
Second Line
import hashlib

#1
output_file_path = "C:/out.txt"
input_file_path = "C:/in.txt"

#2
completed_lines_hash = set()

#3
output_file = open(output_file_path, "w")

#4
for line in open(input_file_path, "r"):
  # 5
hashValue = hashlib.md5(line.rstrip().encode('utf-8')).hexdigest()
#6
  if hashValue not in completed_lines_hash:
    output_file.write(line)
    completed_lines_hash.add(hashValue)
# 7
output_file.close()

Suggestion : 3

Last Updated : 21 Jul, 2022,GATE CS 2021 Syllabus

Without Order = foskerg
With Order = geksfor

Without Order: kogerfs
With Order: g
With Order: ge
With Order: ge
With Order: gek
With Order: geks
With Order: geksf
With Order: geksfo
With Order: geksfor
With Order: geksfor
With Order: geksfor
With Order: geksfor
With Order: geksfor
With Order: geksfor

{
   'a': 1,
   'b': 2,
   'c': 3,
   'd': 4,
   'e': 5
}
OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)])

OrderedDict([('name', None), ('age', None), ('gender', None)])
OrderedDict([('name', 10), ('age', 10), ('gender', 10)])

Suggestion : 4

Note that this normalizes all whitespace characters (so newlines and tabs will be converted as well) and it removes spaces from the ends of our string.,By default the strip method removes all whitespace characters (not just spaces).,If you're trying to remove all sorts of whitespace characters (space, tab, newline, etc.) you could use the string split and join methods:,If we call split on this version string, Python will split on all consecutive whitespace characters:

>>> greeting = " Hello world! " >>>
   no_spaces = greeting.replace(" ", "")
>>> no_spaces
   'Helloworld!'
>>> version = "\tpy 310\n" >>>
   version.split()['py', '310']
>>> no_spaces = "".join(version.split()) >>>
   no_spaces 'py310'
>>>
import re
   >>>
   no_spaces = re.sub(r "\s+", r "", version) >>>
   no_spaces 'py310'
>>> version = "\tpy 310\n" >>>
   normalized_spaces = " ".join(version.split()) >>>
   normalized_spaces 'py 310'