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()