print text without translinelation rule

2

I need to print text from a file on a single line without the line breaks.

def imprime_texto():
with open("texto.txt") as arquivo:
    linhas = arquivo.read().split("\n")
for linha in linhas:
    print(linha.strip(), end=" ")

Entry:

  Programar em python é mui-
  to simples é só praticar.

Correct output: without the dash.

  Programar em python é muito simples é só praticar.

My output is with the stroke and the None I do not know where I'm going wrong in the code.

  Programar em python é mui- to simples é só praticar. None
    
asked by anonymous 17.04.2018 / 21:14

1 answer

3

To remove -

or remove it from the entry

Programar em python é mui 
to simples é só praticar.

or you can use replace

for linha in linhas:
    linha = linha.replace("-", "")
    print(linha.strip(), end=" ")

obs: using replace may be bad as it will remove any entry that has - , eg: prehistory will be prehistory / sub>

Already to remove whitespace that is getting after - , simply change your end to

print(linha.strip(), end="")

Here's how the code went:

def imprime_texto():
    with open("C:\Shared\teste.txt") as arquivo:
        linhas = arquivo.read().split("\n")
    for linha in linhas:
        linha = linha.replace("-", "")
        print(linha.strip(), end="")
imprime_texto()

Output:

  

Programming in python is very simple just practice.

EDIT

As commented on by Miguel there is another solution, which is much better and with less code than the above solution, would be to do every process in reading the file.

def imprime_texto():
    with open("C:\caminho\teste.txt") as arquivo:
        linhas = arquivo.read().replace("-\n", "").replace('\n', "")
        print(linhas)        
imprime_texto()

EDIT 2

We have another solution !!!

Now commented on by Isac

  

"Another interesting solution to remove - is to strip("-") that   also ensures that it only picks up the ones at the end of each line "

def imprime_texto():
    with open("C:\caminho\teste.txt") as arquivo:
        linhas = arquivo.read().split("\n")
    for linha in linhas:
        linha = linha.strip("-")
        print(linha.strip(), end="")
imprime_texto()
    
17.04.2018 / 21:53