python将列表转换为字符串_每日一课 | Python将文件读入列表
讀取日志文件的Python示例,一行一行地進入列表。?
# With '\n', ['1\n', '2\n', '3']
with open('/www/logs/server.log') as?f:
????content = f.readlines()
# No '\n', ['1', '2', '3']
with open('/www/logs/server.log') as?f:
????content = f.read().splitlines()
1.讀取文件->列表
1.1虛擬日志文件
d:\\server.log?
a
b
c
d123
包含1.2 \n?
filename = "d:\\server.log"
with open(filename) as?f:
????lines = f.readlines()print(type(lines))
print(lines)
輸出量?
<class?'list'>
['a\n', 'b\n', 'c\n', 'd\n', '1\n', '2\n', '3']
1.3 \n排除在外
filename = "d:\\server.log"
with open(filename) as?f:????lines = f.read().splitlines()
print(type(lines))
print(lines)
輸出量?
<class?'list'>
['a', 'b', 'c', 'd', '1', '2', '3']
參考文獻:
翻譯自:?https://mkyong.com/python/python-how-to-read-a-file-into-a-list/
推薦閱讀--
每周一課 | Python 示例拆分字符串入到字典里面
每日一課 | 如何將String轉換為int
每日一課 | Python time.sleep 精準延遲到秒
每日一課 | Python 如何判斷一個字符串是否包含另一個字符串?
球分享
球點贊
球在看
總結
以上是生活随笔為你收集整理的python将列表转换为字符串_每日一课 | Python将文件读入列表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rbf神经网络_基于RBF神经网络的监督
- 下一篇: python class 是否存在某个变