How to open a txt file and use to print what I want

Asked

Viewed 546 times

0

I made the following code:

def caixaAlta(texto):
    return texto.upper()

def ultimosNomes(nomeInteiro):
    lista = nomeInteiro.split(' ')
    if lista[-1].lower() == 'junior' or lista[-1].lower()=='sobrinho' \
       or lista[-1].lower()=='neto' or lista[-1].lower()=='filho':
        return caixaAlta(lista[-2])+" "+caixaAlta(lista[-1])
    else:
        return caixaAlta(lista[-1])

def primeirosNomes(nomeInteiro):
    lista = nomeInteiro.split(' ')
    nome=''
    if lista[-1].lower() == 'junior' or lista[-1].lower()=='sobrinho' \
     or lista[-1].lower()=='neto' or lista[-1].lower()=='filho':
        for i in range(len(lista)-2):
            nome = nome + inicial(lista[i])
    else:
        for i in range(len(lista)-1):
            nome = nome + inicial(lista[i])
    return nome

def inicial(nome):
    for i in nome:
        if nome=='da' or nome=='do' or nome=='de' \
           or nome=='das' or nome=='dos' or nome=='e':
            nome=nome.replace(i,".d")
            return nome.lower()+' '

        else:
            return nome[0].upper()

def referencia(nomeInteiro):
    return ultimosNomes(nomeInteiro)+','+primeirosNomes(nomeInteiro)


print(referencia(input('Digite nome do autor: ')))

His exit will be the following:

> Digite nome do autor: Joao Joca da Silva  <- o nome eu digito
> SILVA,JJ.da <- isso que imprime

This way I did I create names and it prints the way I want, but how do I use only names of a txt file and it print the same way?

  • The question is no longer how to read a text file in python line by line ? I question this because the question has a lot of code but it doesn’t seem at all to be related to your doubt.

  • It is that the way I did I would have to ask in the program itself a name to abbreviate. What I am asking instead of :)

1 answer

2


Try So:

Create a file called.txt file with the names you want, so do it:

import  codecs
wordList = codecs.open('Arquivo.txt' , 'r').readlines()
for w in wordlist:
    print(referencia(w[:-1]))

Pythonism:

Practically all languages have conventions and python is no different, it would be interesting if you read python encoding style guide, to quote an example, rather than using camelcase (minhaVariavel), use snakecase (minha_variavel)

In python you don’t need that lot of ifs to see if something is on a list, in the first sequence of ifs of your code, instead of doing:

if lista[-1].lower() == 'junior' or lista[-1].lower()=='sobrinho' \
   or lista[-1].lower()=='neto' or lista[-1].lower()=='filho':
    return caixaAlta(lista[-2])+" "+caixaAlta(lista[-1])
else:
    return caixaAlta(lista[-1])

You can do

if lista[-1].lower() in ['junior', 'sobrinho', 'neto', 'filho']

Note that you can also read this list of a text file, in the same way that was done to read the input file of the names.

Observing.
This is just a way to read a python txt file, I chose randomly (pq had already answered something with codecs) but there are other ways to do this.

For complementary, see also that answer, here at Stopt.

  • Bro thanks a lot but the result came out from underneath each other, would have some way them leave the names line by line?

  • Just remove the \n of the end of the words in Wordlist, I will edit, see q I will make a change in the last line of the first code fragging

  • See if 'funf' now.

  • 1

    It worked out bro thanks a lot!

Browser other questions tagged

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