1

I have a file named "test.txt" which contains the below content: hello hi bye for now

I am using the below code to replace the linebreaks with comma (,) in the file:

with open("test.txt", "r") as data:
    for each_line in data:
        try:
            name = each_line.replace("\n", ",").strip()
            print(name)

        except ValueError:
            pass

This shows the result:

hello,
hi,
bye for now

But my desired result should be:

hello,hi,bye for now

How can i achieve this?

0 Answers0