How can I read or print a block of lines or data from a file, between two lines or two keywords within the file in python?

2

I have several files with the same format and I want to read a block of those files from the line where the numbers or data of the time, voltage and current appear, until the line 'final data', this would be an example or something similar to the archives

File:

Hay chicas chachareras que chacotean con chicos chazos. Y un chico mete al
chillón de la chepa un chichón por chirrichote, y el chiste, y lo 
chocante, es que la chepa se le ha chafado con la hinchazón del chirlo.

Datos del archivo número de variables  4: 

     Tiempo, Voltaje, Corriente
     0.0, 110.0, 6.92820323028
     0.000333333333333, 109.132617145, 6.37223934419
     0.000666666666667, 106.544147724, 5.71578143706
     0.001, 102.275413448, 4.96918224223
     0.00133333333333, 96.3937348048, 4.14421607499
     0.00166666666667, 88.9918693812, 3.25389314461
     0.002, 80.1865490164, 2.31225437556
     0.00233333333333, 70.1166388724, 1.33414997373
     0.00266666666667, 58.9409474477, 0.335005229834
Final de los datos importantes 

Treinta y tres tramos de troncos trozaron tres tristes trocadores de troncos
y triplicaron su trabajo, triplicando su trabajo de trozar troncos y troncos.

Final del archivo.

And I want you to print something like this:

['Tiempo,', 'Voltaje,', 'Corriente', '0.0,', '110.0,', '6.92820323028,',
 '0.000333333333333,', '109.132617145,', '6.37223934419,', 
 '0.000666666666667,', '106.544147724,', '5.71578143706,', '0.001,', 
 '102.275413448,', '4.96918224223,0.00133333333333,', '96.3937348048,', 
 '4.14421607499', '0.00166666666667,', '88.9918693812,', '3.25389314461,', 
 '0.002,', '80.1865490164,', '2.31225437556,', '0.00233333333333,', 
 '70.1166388724,', '1.33414997373,', '0.00266666666667,', '58.9409474477,', 
 '0.335005229834']

Or that you read me only the data and I omit the lines that are before and those that are after these, something like this:

  0.0, 110.0, 6.92820323028
  0.000333333333333, 109.132617145, 6.37223934419
  0.000666666666667, 106.544147724, 5.71578143706
  0.001, 102.275413448, 4.96918224223
  0.00133333333333, 96.3937348048, 4.14421607499
  0.00166666666667, 88.9918693812, 3.25389314461
  0.002, 80.1865490164, 2.31225437556
  0.00233333333333, 70.1166388724, 1.33414997373
 0.00266666666667, 58.9409474477, 0.335005229834

There are several files and they all start in the same way the line 'Data of the file number of variables' appears and the data ends before the line 'End of important data' I had already tried to do with readline () but not it gives me the result

    
asked by Yeison Ordoñez 20.04.2016 в 17:48
source

1 answer

2

We are going to assume that the start and end chains are in the variables inicio and final , respectively. It could be done this way:

inicio = "Datos del archivo"
final = "Final de los datos"

resultado = []

with open("Archivo.txt") as fichero:

    # descartar las primeras líneas
    fpos = (linea for linea in fichero
               if not line.startswith(inicio))

    # recoger las líneas hasta la siguiente marca
    for linea in fpos:
        if linea.startswith(final):
            break
        resultado += linea

In a more elegant and functional way:

from itertools import takewhile

inicio = "Datos del archivo"
final = "Final de los datos"

with open("Archivo.txt") as fichero:

    # descartar las primeras líneas
    fpos = (linea for linea in fichero
               if not line.startswith(inicio))

    # recoger las líneas hasta la siguiente marca
    resultado = list(takewhile(lambda s: not s.startswith(final), fpos))
    
answered by 22.04.2016 / 02:37
source