Python读取txt的三种方法

方法一:


 
 
  1. #read txt method one
  2. f = open( "./image/abc.txt")
  3. line = f.readline()
  4. while line:
  5. print line
  6. line = f.readline()
  7. f.close()

方法二:


 
 
  1. #read txt method two
  2. f = open( "./image/abc.txt")
  3. for line2 in open( "./image/abc.txt"):
  4. print line2



方法三:


 
 
  1. #read txt method three
  2. f2 = open( "./image/abc.txt", "r")
  3. lines = f2.readlines()
  4. for line3 in lines:
  5. print line3


1、如果TXT文件中有两列,可以设定数组,然后分别获取数据

2、上述文件使用的是相对路径,当然也可以使用绝对路径


转自shandong_chu的博客:https://blog.csdn.net/shandong_chu/article/details/70173952

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值