Avatar ·

How to remove newline character (\\n) when reading lines from a file

📁 Advanced Python Topics, AI

When using file.readlines() we get something like this:

>>> file.readlines()['12', '10', '9', '15', '10', '120']

How to remove ?

Avatar ·

As a knee-jerk solution that literally performs the task stated in the question: you can create a class that takes two file objects, and when writing to an object of this class, it writes to both file objects.

from __future__ import print_functionimport sys    class DoubleWrite:    def __init__(self, file1, file2):        self.file1 = file1        self.file2 = file2    def write(self, s):        self.file1.write(s)        self.file2.write(s)    def flush:                                

Log in to leave an answer

Blogs