How to find specific words in a python txt file?

Asked

Viewed 9,721 times

1

I need to make a software that searches for words in the code and saves in a string the next line.
I couldn’t find anything on how to do it anywhere, I just found one that counts the number of words in the file like that

with open('File_of_data.txt', 'r',encoding='utf8') as j:
    for line in j:
        words = line.split('\n')
        num_lines += 1
        num_words += len(words)
print(num_lines)
print(num_words)

After finding the word as the name of a person "Carlos" at the bottom line will be the information of that person and I need to save this to make calculations.

  • I will adhere to the tips :D was worth

  • Wouldn’t it be better to read all this to a dictionary in memory where the keys are people’s names?

  • You can solve this problem with a state machine. But without a sample of the input file, it becomes complicated to help you.

2 answers

2


You can build a simple state machine that performs a line-by-line search of a given input file, searching for the name or one of the person’s surnames.

When finding a search-compatible person, the state machine assumes that the next line to be read contains that person’s data.

Once the line containing the data of the person found, a dictionary can be assembled and returned for general use:

def pesquisar_registro( arq, txt ):
    nome = ""
    with open( arq, 'r' ) as a:
        for linha in a:
            linha = linha.strip('\n')
            if nome == "":
                if txt in linha.split():
                    nome = linha
            else:
                registro = linha.split(',')
                dic = { "nome"       : nome,         \
                        "cod"        : registro[0],  \
                        "pais_nasc"  : registro[1],  \
                        "ano_nasc"   : registro[2],  \
                        "pais_morte" : registro[3],  \
                        "ano_morte"  : registro[4] }
                return dic;
    return None;


print pesquisar_registro( 'fisicos.txt', 'Einstein' )
print pesquisar_registro( 'fisicos.txt', 'Max' )
print pesquisar_registro( 'fisicos.txt', 'Michael' )
print pesquisar_registro( 'fisicos.txt', 'Curie' )
print pesquisar_registro( 'fisicos.txt', 'Obama' )

physique.txt

Albert Einstein
100,Alemanha,1879,EUA,1955
Isaac Newton
200,Reino Unido,1643,Reino Unido,1727
Galileo Galilei
300,Italia,1564,Italia,1642
Marie Curie
400,Polonia,1867,Polonia,1934
Erwin Schrodinger
500,Austia,1887,Austria,1961
Michael Faraday
600,Reino Unido,1791,Reino Unido,1867
Max Planck
700,Alemanha,1858,Alemanha,1947

Exit:

{'ano_nasc': '1879', 'pais_nasc': 'Alemanha', 'nome': 'Albert Einstein', 'ano_morte': '1955', 'cod': '100', 'pais_morte': 'EUA'}
{'ano_nasc': '1858', 'pais_nasc': 'Alemanha', 'nome': 'Max Planck', 'ano_morte': '1947', 'cod': '700', 'pais_morte': 'Alemanha'}
{'ano_nasc': '1791', 'pais_nasc': 'Reino Unido', 'nome': 'Michael Faraday', 'ano_morte': '1867', 'cod': '600', 'pais_morte': 'Reino Unido'}
{'ano_nasc': '1867', 'pais_nasc': 'Polonia', 'nome': 'Marie Curie', 'ano_morte': '1934', 'cod': '400', 'pais_morte': 'Polonia'}
None

2

First thank you Lacobus. Well I needed something similar, but stored differently. I leave here the code adapted by me.

Code:

def pesquisar_registro( txt, posicao ):
resultado = ""
with open( 'arquivo.txt', 'r' ) as a:
    for linha in a:
        if resultado == "":
            if txt in linha:
                resultado = linha
                res = resultado.split('= ')[posicao].strip('\n')
                return res;
    else: a.close()
return None;


print pesquisar_registro( 'modelo', 1 ) # Nome da variavel e posição do resultado dsp do '='

txt file.

marca= Fiat 
modelo= Argo
ano= 2019
placa= AAA-0000
cor= Vermelho

Exit:

Argo

Browser other questions tagged

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