Is it possible to read a text file into a string variable and then scrap the newlines?

0 votes
I have to read a file in Python. suppose my file has multiple lines. Now I want to print all of them in a single line and delete the new lines. How to do it?
Aug 7 in Python by Neel
• 2,730 points
13 views

1 answer to this question.

0 votes

This can be done easily in Python. For example,

with open('data.txt', 'r') as file:
    data = file.read().replace('\n', '')

This code will read the file and print all the content of the file in a single line and replace the new line.

answered Aug 7 by Arvind
• 2,760 points

Related Questions In Python

0 votes
1 answer

Is it possible to print a string and a variable in a single print statement in Python?

This can be done using simple string ...READ MORE

answered Jul 15 in Python by Neel
• 2,730 points
21 views
0 votes
1 answer

Is it possible to run a function in Python using the command line?

Suppose your file name is demo.py and ...READ MORE

answered Jun 26 in Python by Neel
• 2,730 points
42 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 176 views
+4 votes
6 answers
0 votes
1 answer

is it possible to create zip of a directory in Python?

Please go through this code. This should ...READ MORE

answered Jul 10 in Python by Arvind
• 2,760 points
18 views
0 votes
1 answer

Is it possible to create a dictionary with multiple keys for a single value?

One thing that you can do here ...READ MORE

answered Jul 24 in Python by Arvind
• 2,760 points
22 views