Exporting data in json or txt, in python?

Asked

Viewed 9,605 times

0

I need help to learn how to export data from an object. At first I need to export in type "txt" and/or "json", but I could not succeed in either.

The code:

#coding = utf-8
import json

.
.
.
 def arquivo(lista_nomes):
     print(json.dumps(lista_nomes))


 def txt(lista_nomes):
     arq = open('listaNomes.txt', 'w')
     arq.write(for i in lista_nomes:)
     arq.close()

for lista_nome in lista:
    txt(lista_nomes)
    arquivo(lista_nomes)
.
.
.

Trying to export with the "txt" function, I only get a line with only one name, and using json_dumps I don’t have any output(I didn’t quite understand how to use the python lib 'json', I thought it was to export data.)

I also ask how should I make out be in UTF-8 charset? The output, when printing the output in the python compiler, comes with unrecognized characters, so I don’t understand why since the code is 'set' in utf-8.

I’m standing by, guys, to help me solve the problem.

  • How is the structure of your . txt? One name per line? And how and what order are the functions being called in your main file..

  • @Marlysson, yes the text comes a ["name", "lymph formation"]. As far as the functions I believe do not matter, however their call is the same as posted above.

1 answer

1


Come on, one thing at a time.

The module json has two similar but different functions: dump, which accepts the object to be converted and an archive, and dumps, who accepts only the object and returns a string.

To write a json file, therefore, it is more interesting to use the dump:

import json

def escrever_json(lista):
    with open('meu_arquivo.json', 'w') as f:
        json.dump(lista, f)

def carregar_json(arquivo):
    with open('meu_arquivo.json', 'r') as f:
        return json.load(f)

minha_lista = ['João', 'Maria', 'José']
escrever_json(minha_lista)

print(carregar_json('meu_arquivo.json'))  # ['João', 'Maria', 'José']

For text, we use write to write. If we do not include the new line character, all names will be recorded without space, so we have to do this:

def escrever_txt(lista):
    with open('meu_arquivo.txt', 'w', encoding='utf-8') as f:
        for nome in lista:
            f.write(nome + '\n')

def carregar_txt():
    with open('meu_arquivo.txt', 'r', encoding='utf-8') as f:
        return f.readlines()

minha_lista = ['João', 'Maria', 'José']
escrever_txt(minha_lista)

print(carregar_txt())  # ['João\n', 'Maria\n', 'José\n']

If we want the names to come back without the new line character, we can modify the function a little bit:

def carregar_txt():
    with open('meu_arquivo.txt', 'r', encoding='utf-8') as f:
        return [nome.strip() for nome in f.readlines()]
  • Opa pedro, still yes with your solution I am not managing to form txt and json files with subsequent lines, as in a text or book page. I can only generate the first line and save it. This is part of a project, so I need to save object I get by exporting to a ".txt file" and/or ".json file".

Browser other questions tagged

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