Reputation: 3
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
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