Access data and change csv or txt with python

Asked

Viewed 100 times

0

I have this script that does data negotiations, I took some examples from the internet but could not access the data, before recording in csv I need to modify some fields, for example, the field date, is separated by point(24.03.2020) and need to pass save (24/03/2020), I thought about doing replace but I haven’t been able to access the data, could help me?

    with open("ZV_R0051.txt", "r", encoding="UTF-16") as ler, open("teste.csv", mode='w', encoding='UTF-16') as gravar:
        next(ler)
        next(ler)
        gravar.writelines(['data_puxada;','centro;','atribuicao;','ped_pca;','transporte;','fornecimento;','codigo_material;','descr_produto;','descr_status_pedido;','quantidade;','hora_puxada;','cliente;','cliente_sap;','numero_nota_fiscal;','data_inicio_carregamento;','hora_inicio_carregamento;','dt_termino_carregamento;','hora_termino_carregamento;','numeroov_pedtransf;','can_distrib;','tipo_operacao;','tipo_operacao;'])
        gravar.write('\n')
        next(ler)
        next(ler)

        for linha in ler:
            if  len(linha.strip()) > 0 and len(linha.strip("਍ഀ").strip()) > 0:
                linha = linha.rstrip()
                gravar.writelines(linha.replace('\t',';').strip()[:-2].rstrip()[1:])
                gravar.write('\n')
                print(linha)

except AttributeError:
    print("Erro")

1 answer

0

I got,

added . replace("." ,'/',2). replace("." ,"",10)

where is the number tells which column I want to change. So it was like this

write.writelines(line.replace(' t',';'). strip()[:-2]. rstrip()[1:]. replace("." ,'/',2). replace("." ,"",10,))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.