Python - write() versus writelines() and concatenated strings

前端 未结 5 1948
暖寄归人
暖寄归人 2020-11-27 10:54

So I\'m learning Python. I am going through the lessons and ran into a problem where I had to condense a great many target.write() into a single write()

5条回答
  •  长情又很酷
    2020-11-27 11:31

    • writelines expects an iterable of strings
    • write expects a single string.

    line1 + "\n" + line2 merges those strings together into a single string before passing it to write.

    Note that if you have many lines, you may want to use "\n".join(list_of_lines).

提交回复
热议问题