0 votes
1 view
in Python by (1.6k points)

I used the below code to read a file in Python:

with open ("data.txt", "r") as myfile:

    data=myfile.readlines()

Input file is:

LLKKKKKKKKMMMMMMMMNNNNNNNNNNNNN

GGGGGGGGGHHHHHHHHHHHHHHHHHHHHEEEEEEEE

and when I print data I get

['LLKKKKKKKKMMMMMMMMNNNNNNNNNNNNN\n', 'GGGGGGGGGHHHHHHHHHHHHHHHHHHHHEEEEEEEE']

As I see data is in list form. How do I make it string? Also, how can I remove the characters, "\n", "[", and "]" from this output?

2 Answers

0 votes
by (25.1k points)

You could use:

with open('data.txt', 'r') as f:

    results = f.read().replace('\n', '')

0 votes
by (108k points)

You can read from a file in one line in Python:

str = open('very_Important.txt', 'r').read()

Please note that this does not close the file explicitly.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 11, 2020 in Python by ashely (50.2k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !

Categories

...