Richard Rublev
Richard Rublev

Reputation: 8164

How to read this file,with skipping some lines using Python?

I need to skip 47 lines of the file(header and so on),then read this

 4.163186002791e+04  3.578830331359e+04  3.076496349687e+04  2.644671278966e+04  2.273458304119e+04 
 1.954349752908e+04  1.680032112209e+04  1.444218412726e+04  1.241504140604e+04  1.067243373686e+04 
 9.174423035938e+03  7.886677033340e+03  6.779682426302e+03  5.828068476394e+03  5.010025548360e+03 
 1.737988920100e+03  1.284332855871e+03  1.104060538508e+03  8.158747205330e+02  7.013564117662e+02 
 6.029121922103e+02  5.182858606802e+02  4.455379022877e+02  2.433020871700e+02  2.091515701348e+02 
 1.797945089525e+02  1.545580816278e+02  1.328639052196e+02  9.818329499070e+01  7.255514128762e+01 
 5.361653963401e+01  4.609078195788e+01  3.962135930423e+01  3.406000172766e+01  2.927925083995e+01 
 2.516953864546e+01  2.163667639887e+01  1.859969593339e+01  1.598899398582e+01  1.374473698894e+01 
 1.181548977143e+01  1.015703673713e+01  8.731368506527e+00  7.505810795983e+00  6.452275569743e+00 
 5.546617302183e+00  4.768079596776e+00  4.098819479081e+00  3.523498461194e+00  3.028931005477e+00 
 2.603782330822e+00  2.238308635635e+00  1.924133783786e+00  1.654057335509e+00  1.421889523591e+00 
 1.222309392724e+00  1.050742850800e+00  9.032578372386e-01  7.764742057598e-01  6.674862562538e-01 
 5.737961402745e-01  4.932566139141e-01  3.133421372728e-01  2.315524554696e-01  1.990511474577e-01 
 1.711118080085e-01  1.470941072881e-01  1.264475938317e-01  1.086990789815e-01  9.344179207682e-02 
 8.032605785014e-02  6.905128236880e-02  5.935906385039e-02  5.102727046220e-02 

possibly as list,and then again skip 21 lines and read the part of the file in same format as presented above. Firstly my idea was something like this:

from itertools import islice
n=15
with open('91_FULLMERGED.edi') as f:
    lines_after_48 = f.readlines()[48:]
    while True:
        next_15_lines = list(islice(lines_after_48, n))
        if not next_15_lines:
            break

But that is not working.

milenko@milenko-HP-Compaq-6830s:~/EDIs$ python k1.py 

It is just standing still in terminal.

How to solve this?

Upvotes: 0

Views: 85

Answers (2)

Kordi
Kordi

Reputation: 2465

Code

I use the csv reader cause the format is csv with delimiter ' '. At first I just skipped 47 lines by just next(f,None). Afterwards the csv module is making the trick. If you want to output as a file you can just use the csv writer. You can uncomment the code if you want to remove the empty strings from the output list. But then the output to the file is not similar to the input. It depends on how you wanna use the data.

import csv

with open('input.txt', 'rb') as f:
    for i in range(47):
        next(f, None)
    reader = csv.reader(f,delimiter=' ')
    values = list(reader)

# if you want to remove the ''
#for idx, val in enumerate(values):
#    values[idx] = [x for x in values[idx] if x != '']

print values

with open('output.txt', 'wb') as f:
    writer = csv.writer(f, delimiter=' ', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    for line in values:
        writer.writerow(line)    

Output to screen

[['4.163186002791e+04', '', '3.578830331359e+04', '', '3.076496349687e+04', '', '2.644671278966e+04', '', '2.273458304119e+04'],
['1.954349752908e+04', '', '1.680032112209e+04', '', '1.444218412726e+04', '', '1.241504140604e+04', '', '1.067243373686e+04'],
['9.174423035938e+03', '', '7.886677033340e+03', '', '6.779682426302e+03', '', '5.828068476394e+03', '', '5.010025548360e+03'],
['1.737988920100e+03', '', '1.284332855871e+03', '', '1.104060538508e+03', '', '8.158747205330e+02', '', '7.013564117662e+02'],
['6.029121922103e+02', '', '5.182858606802e+02', '', '4.455379022877e+02', '', '2.433020871700e+02', '', '2.091515701348e+02'],
['1.797945089525e+02', '', '1.545580816278e+02', '', '1.328639052196e+02', '', '9.818329499070e+01', '', '7.255514128762e+01'],
['5.361653963401e+01', '', '4.609078195788e+01', '', '3.962135930423e+01', '', '3.406000172766e+01', '', '2.927925083995e+01'],
['2.516953864546e+01', '', '2.163667639887e+01', '', '1.859969593339e+01', '', '1.598899398582e+01', '', '1.374473698894e+01'],
['1.181548977143e+01', '', '1.015703673713e+01', '', '8.731368506527e+00', '', '7.505810795983e+00', '', '6.452275569743e+00'],
['5.546617302183e+00', '', '4.768079596776e+00', '', '4.098819479081e+00', '', '3.523498461194e+00', '', '3.028931005477e+00'],
['2.603782330822e+00', '', '2.238308635635e+00', '', '1.924133783786e+00', '', '1.654057335509e+00', '', '1.421889523591e+00'],
['1.222309392724e+00', '', '1.050742850800e+00', '', '9.032578372386e-01', '', '7.764742057598e-01', '', '6.674862562538e-01'],
['5.737961402745e-01', '', '4.932566139141e-01', '', '3.133421372728e-01', '', '2.315524554696e-01', '', '1.990511474577e-01'],
['1.711118080085e-01', '', '1.470941072881e-01', '', '1.264475938317e-01', '', '1.086990789815e-01', '', '9.344179207682e-02'],
['8.032605785014e-02', '', '6.905128236880e-02', '', '5.935906385039e-02', '', '5.102727046220e-02']]

Output to file output.txt

4.163186002791e+04  3.578830331359e+04  3.076496349687e+04  2.644671278966e+04  2.273458304119e+04
1.954349752908e+04  1.680032112209e+04  1.444218412726e+04  1.241504140604e+04  1.067243373686e+04
9.174423035938e+03  7.886677033340e+03  6.779682426302e+03  5.828068476394e+03  5.010025548360e+03
1.737988920100e+03  1.284332855871e+03  1.104060538508e+03  8.158747205330e+02  7.013564117662e+02
6.029121922103e+02  5.182858606802e+02  4.455379022877e+02  2.433020871700e+02  2.091515701348e+02
1.797945089525e+02  1.545580816278e+02  1.328639052196e+02  9.818329499070e+01  7.255514128762e+01
5.361653963401e+01  4.609078195788e+01  3.962135930423e+01  3.406000172766e+01  2.927925083995e+01
2.516953864546e+01  2.163667639887e+01  1.859969593339e+01  1.598899398582e+01  1.374473698894e+01
1.181548977143e+01  1.015703673713e+01  8.731368506527e+00  7.505810795983e+00  6.452275569743e+00
5.546617302183e+00  4.768079596776e+00  4.098819479081e+00  3.523498461194e+00  3.028931005477e+00
2.603782330822e+00  2.238308635635e+00  1.924133783786e+00  1.654057335509e+00  1.421889523591e+00
1.222309392724e+00  1.050742850800e+00  9.032578372386e-01  7.764742057598e-01  6.674862562538e-01
5.737961402745e-01  4.932566139141e-01  3.133421372728e-01  2.315524554696e-01  1.990511474577e-01
1.711118080085e-01  1.470941072881e-01  1.264475938317e-01  1.086990789815e-01  9.344179207682e-02
8.032605785014e-02  6.905128236880e-02  5.935906385039e-02  5.102727046220e-02

My Input Values I used

SKIP (for line 1 ... removed 45 Lines for better readablity
SKIP  to line 47)
4.163186002791e+04  3.578830331359e+04  3.076496349687e+04  2.644671278966e+04  2.273458304119e+04
1.954349752908e+04  1.680032112209e+04  1.444218412726e+04  1.241504140604e+04  1.067243373686e+04
9.174423035938e+03  7.886677033340e+03  6.779682426302e+03  5.828068476394e+03  5.010025548360e+03
1.737988920100e+03  1.284332855871e+03  1.104060538508e+03  8.158747205330e+02  7.013564117662e+02
6.029121922103e+02  5.182858606802e+02  4.455379022877e+02  2.433020871700e+02  2.091515701348e+02
1.797945089525e+02  1.545580816278e+02  1.328639052196e+02  9.818329499070e+01  7.255514128762e+01
5.361653963401e+01  4.609078195788e+01  3.962135930423e+01  3.406000172766e+01  2.927925083995e+01
2.516953864546e+01  2.163667639887e+01  1.859969593339e+01  1.598899398582e+01  1.374473698894e+01
1.181548977143e+01  1.015703673713e+01  8.731368506527e+00  7.505810795983e+00  6.452275569743e+00
5.546617302183e+00  4.768079596776e+00  4.098819479081e+00  3.523498461194e+00  3.028931005477e+00
2.603782330822e+00  2.238308635635e+00  1.924133783786e+00  1.654057335509e+00  1.421889523591e+00
1.222309392724e+00  1.050742850800e+00  9.032578372386e-01  7.764742057598e-01  6.674862562538e-01
5.737961402745e-01  4.932566139141e-01  3.133421372728e-01  2.315524554696e-01  1.990511474577e-01
1.711118080085e-01  1.470941072881e-01  1.264475938317e-01  1.086990789815e-01  9.344179207682e-02
8.032605785014e-02  6.905128236880e-02  5.935906385039e-02  5.102727046220e-02

Upvotes: 1

marmeladze
marmeladze

Reputation: 6564

give a chance to linecache. and in case of large files, try generators.

import linecache
from sys import argv

def get_specific_lines(f, start, stop):
    for i in range(start, stop):
        yield linecache.getline(f, i)

script, f, s, e = argv

# check code if (f)ile exists, (s)tart and (e)nd are numeric.    

for line in get_specific_lines(f, int(s), int(e)):
    print line



$ wc -l asbc.txt 
197310 asbc.txt
$ python read.py asbc.txt 144322 144325
Bu yarım dəqiqə ərzində Qleb qabaqcadan dəqiq hesablanmış ləng hərəkətlə əlini qəbulediciyə uzatdı, diktora xırıldamağa imkan vermədən boynunu bururmuş kimi açarın dəstəyini burdu.

Onun azca bundan əvvəlki canlı sifəti yorğun, bozumtul rəng almışdı.

Pryançikovun başı isə başqa problemə qarışmışdı. Hansı gücləndirici kaskadı qoymaq lazım olduğu haqda fikirləşə-fikirləşə o, qayğısız zümzümə edirdi:

Upvotes: 0

Related Questions