Hi guys how are you? I hope you just fine! How to parse a text file extracting specific values using index positions, append the values to a list, then convert it to pandas dataframe. So far I was to able write the below code: TEXT SAMPLE:
header:0RCPF049100000084220210407
body:1927907801100032G 00sucess
1067697546140032G 00sucess
1053756666000032G 00sucess
1321723368900032G 00sucess
1037673956810032G 00sucess
For example, the first line is the header, and from it, I just need the date which is in the following index position: date_from_header = linhas[0][18:26] The rest of the values is in body
import csv
import pandas as pd
headers = ["data_mov", "chave_detalhe", "cpf_cliente", "cd_clube",
"cd_operacao","filler","cd_retorno","tc_recusa"]
# This is the actual code
with open('RCPF0491.20210407.1609.txt', "r")as f:
linhas = [linha.rstrip() for linha in f.readlines()]
for i in range(0,len(linhas)):
data_mov = linhas[0][18:26]
chave_detalhe=linhas[1][0:1]
cpf_cliente=linhas[1][1:12]
cd_clube=linhas[1][12:16]
cd_operacao=linhas[1][16:17]
filler=linhas[1][17:40]
cd_retorno=linhas[1][40:42]
tx_recusa=linhas[1][42:100]
data = [data_mov,chave_detalhe,cpf_cliente,cd_clube,cd_operacao","filler,cd_retorno,tc_recusa]
The intended result looks like this:
data_mov chave_detalhe cpf_cliente cd_clube cd_operacao filler cd_retorno tx_recusa
'20210407' '1' 92790780110 '0032' 'G' 'blank space' '00' 'sucesso'
'20210407' '1' 92790780110 '0032' 'G' 'blank space' '00' 'sucesso'
'20210407' '1' 92790780110 '0032' 'G' 'blank space' '00' 'sucesso'