Printing words or phrases from a text

Asked

Viewed 65 times

0

Hello, guys, I’m a beginner in Python. I’m learning alone and I’ve done some programs, which is pretty cool. But I’m trying to make a new program and I’m not getting it. Here’s the thing:

I have a long text, like:

'''
Meu nome é João.
Eu moro no Brasil. Eu tenho nível superior...
'''

Therefore, I wish to collect or print a few words or phrases.

The text is very long and, as I read, I discover the sentences I want. So, I thought I’d mark the desired phrase, like, starting and ending with '**':

'''
Meu nome é **João**.
Eu **moro no Brasil**.
Eu tenho **nível superior**...
'''

Once this is done, I want to collect/print every sentence that starts and ends with '**': like:

'João'
'moro no Brasil'
'nível superior'
...

How can I do that?

  • can do a . split(' n'), this will cut your str at the end-of-line points, and return in list format

  • Elton Nunes, thank you very much for the tip of the line break. Thus, I managed to isolate the desired stretches.

2 answers

0

I thank my colleagues for the tips sent. After analyzing each of them, I was able to develop a code. It was as follows:

txt_inicial = '''Meu nome é <João>. Eu <moro no Brasil>. 
Eu tenho <nível superior>...''' # Texto inicial. Na medida em que eu vou lendo, vou selecionando o trecho desejado, isolando-o com o uso de < e >.

quebra1 = txt_inicial.replace('<', '\n#') # Na primeira quebra de linha, começei a isolar o trecho desejado em uma nova linha. Inseri # para separar as linhas desejas, ou seja, as linhas que contém o trecho que desejo imprimir ao final.
quebra2 = quebra1.replace('>', '\n') #Na segunda quebra de linha, termino de isolar o trecho desejado.

trechos_isolados = quebra2 #Trechos isolados.

txt_final = trechos_isolados.split('\n') #Colocando os trechos isolados em uma lista.

for trecho in txt_final:
  if '#' in trecho:
    print(trecho) # Trechos desejados. Se quiser retirar o #, então substitua por: print(trecho.replace('#', '')) 

-1

First, you could use "replace()" to remove the spaces and dots between words:

txt = "Meu nome é João. Eu moro no Brasil. Eu tenho nível superior..."
txt.replace(' .','')

In this case, it will replace the spaces and dots (as shown between the 1°s quotation marks) with nothing (as shown between the 2°s quotation marks). Then you’ll have something like:

"MeunomeéJoãoEumoronoBrasilEutenhonívelsuperior"

You can add this to a variable, like:

text = txt.replace(' .','')
text = MeunomeéJoãoEumoronoBrasilEutenhonívelsuperior

Hence you use split() to split the text word by word. If the result is a list, just print() the items you want:

texto = text.split()
texto = "Meu", "nome", "é", "João", "Eu", "moro", "no", "Brasil" "Eu", "tenho", "nível", "superior".
print(texto[3], texto[5], texto[6], texto[7], texto[10], texto[11]')

Your result will be like:

João moro no Brasil nível superior
  • Thulera, thanks for your tip, the way you put it, I had to know the contents of each excerpt and, as the text is too large, would be unviable, at least the way I understood your tip.

Browser other questions tagged

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