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, 2019 in Python by Neel
• 3,020 points
1,731 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, 2019 by Arvind
• 3,050 points

Related Questions In Python

0 votes
1 answer

How to read a text file into a string variable and strip newlines?

You could use: with open('data.txt', 'r') as file: ...READ MORE

answered Dec 23, 2020 in Python by Gitika
• 65,770 points
654 views
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, 2019 in Python by Neel
• 3,020 points
828 views
–1 vote
2 answers
0 votes
1 answer
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,069 views
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, 2019 in Python by Arvind
• 3,050 points
893 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, 2019 in Python by Arvind
• 3,050 points
735 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP