python逐行读取文件内容的三种方法
方法一:
f = open("foo.txt") # 返回一个文件对象 line = f.readline() # 调用文件的 readline()方法 while line: print line, # 后面跟 ‘,‘ 将忽略换行符 # print(line, end = ‘‘) # 在 Python 3中使用 line = f.readline() f.close()
方法二:
for line in open("foo.txt"): print line,
方法三:
f = open("c:\\1.txt","r") lines = f.readlines()#读取全部内容 for line in lines print line
相关推荐
dflyzx 2020-09-17
higheels 2020-07-20
Ericbig 2020-07-19
yinyang00 2020-05-15
lijiuchangxin 2020-05-14
yogoma 2020-05-09
jacktangj 2020-04-19
Yyqingmofeige 2020-03-26
idning 2020-02-14
宿舍 2020-01-31
luenxin 2020-01-22
水痕 2020-01-18
secondid 2019-12-18
XCodeRush 2019-12-17
ryuhfxz 2019-11-20
qonsnow 2014-03-07
coulder 2019-11-17
wangxiaohua 2015-05-09