How to read a text file into a string variable and strip newlines?

前端 未结 23 2400
醉酒成梦
醉酒成梦 2020-11-22 05:47

I use the following code segment to read a file in python:

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

Input fil

23条回答
  •  攒了一身酷
    2020-11-22 06:19

    Maybe you could try this? I use this in my programs.

    Data= open ('data.txt', 'r')
    data = Data.readlines()
    for i in range(len(data)):
        data[i] = data[i].strip()+ ' '
    data = ''.join(data).strip()
    

提交回复
热议问题