Return of python file read function

Asked

Viewed 309 times

1

I did this snippet of code to read through a loop the contents of the list files.

lista_nome_base_docs = ['a.txt', 'b.txt', 'c.txt']
tamanho = len(lista_nome_base_docs)
print (tamanho)

lista_geral_arquivos = []


for i in range(tamanho):
     with open(lista_nome_base_docs[i],"r") as arquivo:
     conteudo = [line.strip() for line in arquivo if line.strip() != "" and line.strip() != "." and line.strip() != "\n" and line.strip() != "!" and line.strip() != "?" and line.strip() != ":" and line.strip() != "," and line.strip() != ""]
     lista_geral_arquivos.append(conteudo)

print (lista_geral_arquivos)

but my list list gets this content:

[['€\x03]q\x00]q\x01a.'], ['€\x03]q\x00]q\x01a.'], ['€\x03]q\x00]q\x01a.']]

does anyone know how the words appear instead of these strange characters? I have tried the .read() and .readlines() and they’re not putting each word in a list position, but this bunch of jumbled characters...

  • What is the content of these files?

  • @Andersoncarloswoss are just phrases with , - : - . and ? in the middle of words, a very simple text

  • for i in range(tamanho):
 with open(lista_nome_base_docs[i],"r") as arquivo:
 conteudo = arquivo.readlines()
 lista_geral_arquivos.append(conteudo)

print (lista_geral_arquivos) doing so tbm reads a lot of strange thing @Andersoncarloswoss

  • @Andersoncarloswoss got here.... after you asked me about the contents of the files I went to check, and I do not know why, the sentence was exchanged for characters in Japanese hahaha, I switched the content and it worked

1 answer

2


How about:

arquivos = ['a.txt', 'b.txt', 'c.txt']
lista_geral = []

for nome in arquivos:

    with open( nome, "r") as arq:

        a = []

        for linha in arq:
            a.append(linha.strip())

    lista_geral.append(a)

print( lista_geral )

Browser other questions tagged

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