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

猜你喜欢

转载自blog.csdn.net/qq_38228254/article/details/83619167