嗨,伙计们,你好吗?我希望你没事! 如何解析使用索引位置提取特定值的文本文件,将值附加到列表,然后将其转换为dataframe。到目前为止,我能够编写以下代码: 文本示例:
header:0RCPF049100000084220210407
body:1927907801100032G 00sucess
1067697546140032G 00sucess
1053756666000032G 00sucess
1321723368900032G 00sucess
1037673956810032G 00sucess
例如,第一行是标题,从中,我只需要位于以下索引位置的日期: date_from_header=linhas[0][18:26] 其余的值在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]
预期结果如下所示:
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'
我感谢SamBob的帮助,以下是最终解决方案,以防任何人需要:
使用stackoverflow.com/a/10851479/1581658
相关问题 更多 >
编程相关推荐