如何將文本文件讀入字符串變量並刪除換行符? - 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
相關文章
相關標籤/搜索