How to correlate files from two txt files in Python

Asked

Viewed 407 times

3

I would like to know a way to correlate equal words in different txt files. It will read the words of a txt file and look for those words in the other txt file.

2 answers

2


To see the elements in common between two files (word lists) makes the intersection between your sets:

with open('arquivo1.txt') as f1, open('arquivo2.txt') as f2:
    content1 = f1.read().split() # dividir por palavras
    content2 = f2.read().split() # dividir por palavras
comuns = set(content1) & set(content2) # {paravras, comuns, nos, dois, arquivos}

Python sets

DEMONSTRATION

  • mano to creating a list of objects here... but when I print this list of objects is giving a strange exit... you know how to do this? lista_Objetos = []&#xA;&#xA;&#xA;def cria_lista_objetos(word,indiceArq,qtd):&#xA; lista_ObjetosFun = []&#xA; nomeWord = PalavraIndices(word)&#xA; nomeWord.add_item(indiceArq,qtd)&#xA; lista_ObjetosFun.append(nomeWord)&#xA; return lista_ObjetosFun&#xA;&#xA;for i in palavras_A:&#xA; lista_Objetos = cria_lista_objetos(i,'arquivo1',1) and this is the way out: [<__main__.PalavraIndices object at 0x02B39D50>]

  • I’ll put as a question then... here in the comment it is difficult to even understand..

  • I thought when printase the Objects list would show the name of each instantiated object inside the list... and then I would write to a file.. each object of that, going through the list

  • @Williamhenrique but you have only one object in the list. When lista_Objetos = cria_lista_objetos(i,'arquivo1',1), you are simply replacing the value. you must want to lista_Objetos.append(...)

  • I went up a question there.. because I tried to understand here and I could not... from a look there.. name of the question is: List of objects in Python

0

Well... I’d do it this way:

First it would save the contents of each file in a list, using lista = file.read(), then make a newlista = lista.split() which would take each word and place it in a list position, which would result in something like this:

conjunto_A = ['oi','tchaum','ontem','amanha']
conjunto_B = ['oi','tchaum','ontem','amanha','tarde','noite']

palavras_C = [item for item in conjunto_A if item  in conjunto_B]
print(palavras_C)

having word lists is just checking which words on list A also belong to the word set on list B

in this example I gave, the output is:

['oi', 'tchaum', 'ontem', 'amanha']

only the elements that the two lists have.

Browser other questions tagged

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