问题:
I use the following code segment to read a file in python: 我使用以下代码段在python中读取文件:
with open ("data.txt", "r") as myfile:
data=myfile.readlines()
Input file is: 输入文件为:
LLKKKKKKKKMMMMMMMMNNNNNNNNNNNNN
GGGGGGGGGHHHHHHHHHHHHHHHHHHHHEEEEEEEE
and when I print data I get 当我打印数据时
['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"
, "["
和"]"
字符?
解决方案:
参考一: https://stackoom.com/question/Z7DP/如何将文本文件读入字符串变量并删除换行符参考二: https://oldbug.net/q/Z7DP/How-to-read-a-text-file-into-a-string-variable-and-strip-newlines
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/3274454