如何将文本文件读入字符串变量并删除换行符? - How to read a text file into a string variable and strip newlines?

问题:

I use the following code segment to read a file in python: 我使用如下代码段在python中读取文件: python

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

Input file is: 输入文件为: ide

LLKKKKKKKKMMMMMMMMNNNNNNNNNNNNN
GGGGGGGGGHHHHHHHHHHHHHHHHHHHHEEEEEEEE

and when I print data I get 当我打印数据时 spa

['LLKKKKKKKKMMMMMMMMNNNNNNNNNNNNN\n', 'GGGGGGGGGHHHHHHHHHHHHHHHHHHHHEEEEEEEE']

As I see data is in list form. 如我所见,数据是list形式的。 How do I make it string? 我如何使其成为字符串? And also how do I remove the "\\n" , "[" , and "]" characters from it? 还有如何从中删除"\\n""[""]"字符? .net


解决方案:

参考一: https://stackoom.com/question/Z7DP/如何将文本文件读入字符串变量并删除换行符
参考二: https://oldbug.net/q/Z7DP/How-to-read-a-text-file-into-a-string-variable-and-strip-newlines