dima99816
dima99816

Reputation: 3

Can't replace \n to ,

Example:
blahblahblah
blahblahblah
blahblahblah

Turn to:
blahblahblah, blahblahblah, blahblahblah

with open('a.txt', 'r') as f:
    temp = f.readlines()

for line in temp:
    line.replace('\n', ', ', end='')

print(temp)

input()

Upvotes: 0

Views: 87

Answers (1)

Sup
Sup

Reputation: 331

Using read() is better for your case instead of readlines(). You can do the following:

with open('a.txt', 'r') as f:
    temp = f.read().replace("\n", ",")
print(temp)

Edit: In case there is a new line in the end, the above code would give the output as:

blahblahblah, blahblahblah, blahblahblah,

In order to remove the last comma, you can simply use temp[:-1]

Upvotes: 2

Related Questions