How to save data to a file?

Asked

Viewed 48 times

0

I am consuming a Facebook business API and need to put the data in a TXT or CSV file.

When I print the result with print, comes out as expected, but I can’t put the lines in a txt file.

Exit with print:

[Anhanguera] - VALINHOS/SP - Saldo : R$ 5755
[Anhanguera] - PASSO FUNDO/RS - Saldo : R$ 58157
[Anhanguera] - CAXIAS DO SUL/RS - Saldo : R$ 108708
[Anhanguera] - JARAGUA DO SUL/SC - Saldo : R$ 117410
[Anhanguera] - RIO CLARO/SP - Saldo : R$ 36804
[Pitagoras] - JABOATAO DOS GUARARAPES/PE - Saldo : R$ 40744
[Pitagoras] - IRECE/BA - Saldo : R$ 59997
[Anhanguera] - SAO JOSE DO RIO PRETO/SP - Saldo : R$ 34776
[Pitagoras] - JUIZ DE FORA/MG - Saldo : R$ 45540
[Anhanguera] - MANAUS/AM - Saldo : R$ 118413
[Anhanguera] - ERECHIM/RS - Saldo : R$ 50044
[Anhanguera] - NOVA IGUACU/RJ - Saldo : R$ 103828
[Anhanguera] - FRANCA/SP - Saldo : R$ 18543
[Pitagoras] - ITABIRA/MG - Saldo : R$ 118318
[Anhanguera] - ASSIS/SP - Saldo : R$ 47249

I need to put this data in a TXT file as well as is presented.

1 answer

0


To solve this problem, you must create a file, and write the output of your print in the file. In my example inputdata would be your data array

with open('data.txt', 'w') as f:
    for linha in inputdata:
        f.write(linha)
    
    

In this example the parameter w means that we are creating a file and wish to write data.

I recommend the documentation to understand how the open command works, and check other options for writing to file.

Open documentation

  • Thank you very much!!!

Browser other questions tagged

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