Python读取txt文件,有两种方式:
(1)逐行读取
1 data=open("data.txt") 2 line=data.readline() 3 while line: 4 print line 5 line=data.readline()
(2)一次全部读入内存
1 data=open("data.txt") 2 for line in data.readlines(): 3 print line
时间: 2024-10-26 05:22:04
Python读取txt文件,有两种方式:
(1)逐行读取
1 data=open("data.txt") 2 line=data.readline() 3 while line: 4 print line 5 line=data.readline()
(2)一次全部读入内存
1 data=open("data.txt") 2 for line in data.readlines(): 3 print line