循环读取文件的内容:
python2的写法
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 打开文件
fo = open("jb51.txt", "r")
print "文件名为: ", fo.name
for line in fo.readlines(): #依次读取每行
line = line.strip() #去掉每行头尾空白
print "读取的数据为: %s" % (line)
# 关闭文件
fo.close()python3的写法
# -*- coding: utf-8 -*-
# 打开文件
fo = open("jb51.txt", "r")
print("文件名为: ",fo.name)
for line in fo.readlines(): #依次读取每行
line = line.strip() #去掉每行头尾空白
print ("读取的数据为: %s" % (line))
# 关闭文件
fo.close()效果如下图所示
Copyright © 2019- ltcredit.com 版权所有
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务