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 怎么将数组转为列表_Python怎么将文件读入列表?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WPF – 使用触发器
- 下一篇: 小说网jsp源码_基于jsp+mysql