如图多行的数据文件读入,直接上代码。
方法一:
for line in open(r'C:\Users\Desktop\stat.txt'):
print(line)
方法二:
f = open (r'C:\Users\Desktop\stat.txt','r')
lines = f.readlines()
for line in lines:
print(line)
方法三:
f = open(r'C:\Users\Desktop\stat.txt')
line = f.readline()
while line:
print(line,) #line后面的‘,’将忽略换行符
line = f.readline()
f.close()
运行结果节选: