如何用python读取文本中指定行的内容

1 利用python的readlines()函数:   

   fobj = open(r'Ori.Data.txt','r')
    for line in fobj.readlines()[1000:]
    fobj.close()

2  利用 linecache

   import linecache
   print(linecache.getline(r'D:\z.txt',10))

3 读取10行到13行中的内容

    lnum = 0
    with open('pit.txt', 'r') as fd:
        for line in fd:
            lnum += 1;
            if (lnum >= 10) && (lnum <= 13):
                print line
    fd.close()

4 求文本的行数

 
    fobj = open('Ori_Data.txt','r')
    row_len = len(fobj.readlines())  

     fobj = open(filepath,'r')
     data = fobj.read()
     fobj.close()
     text_len = data.count('\n')

你可能感兴趣的:(Python)