
Extraer fragmentos de texto de un archivo iterativamente
Publicado por Miguel (8 intervenciones) el 16/07/2021 18:37:51
Hola lo que pasa estoy generando un programa que contiene muchos datos y debo extraer el campo startime_local con su valor y guardarlo en otro archivo este campo se repite muchas veces, ejemplo:
call starttime="1626272690029" starttime_local="2021-07-14T09:24:50-0500" endtime="1626272695584"
call starttime="1626272694256" starttime_local="2021-07-14T09:24:54-0500" endtime="1626272695584"
call starttime="1626272677635" starttime_local="2021-07-14T09:24:37-0500" endtime="1626272695589"
y asis demasiadas veces lo intente como se muestra abajo pero no funciona alguna sugerencia de como modificar mi código.
call starttime="1626272690029" starttime_local="2021-07-14T09:24:50-0500" endtime="1626272695584"
call starttime="1626272694256" starttime_local="2021-07-14T09:24:54-0500" endtime="1626272695584"
call starttime="1626272677635" starttime_local="2021-07-14T09:24:37-0500" endtime="1626272695589"
y asis demasiadas veces lo intente como se muestra abajo pero no funciona alguna sugerencia de como modificar mi código.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import re
import msvcrt
import time
f = open ('SBC_IX_MX1XML_2021-07-14_14h24_55_UTC','r')
mensaje = f.read()
regex=r'starttime_local=' #Busca el delimitador inicial
regex2=r'"' #Busca el delimitador final
print(regex)
print(regex2)
coincidencia = re.finditer(regex, mensaje)
coincidencia2 = re.finditer(regex2, mensaje)
for matchNum, match in enumerate(coincidencia):
for matchNum1, match1 in enumerate(coincidencia2):
print (match.group())
split_1 = mensaje.split(match.group())
print( match1.group())
text = ' '.join(' '.join(split_1).strip().split(match1.group()))
final_file = open("Datos"+".txt", "w")
final_file.write(text)
final_file.close()
text=""
break
matchNum1 = matchNum1 + 1
matchNum = matchNum + 1
print("Presione una tecla para continuar...")
msvcrt.getch()
Valora esta pregunta


0