Python Forum

Full Version: Simple script writted by a dumb dude, myself
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good night, guys! I really need your help.
This is the code:

def abreArquivo():
    while True:
        try:
            nome = raw_input('Entre com o nome do arquivo: ')
            arqIn = open(nome)
            return arqIn
        except IOError:
            print 'O arquivo informado não existe.'

arqIn = abreArquivo()

listaOrigem = []
while True:
    lista = []
    i = 0
    linha = arqIn.readline()
    if linha == '': break
    for i in range(len(linha)):
        if linha[i] == "-":
            lista.pop()
            lista = ''.join(lista)
            listaOrigem.append(lista)
            break
        else: lista.append(linha[i])

listaDestino = []
while True:
    lista = []
    j = 0
    linha = arqIn.readline()
    print len(linha)
    if linha == '': break
    if len(linha) > 5:
        while linha[j] != ">": j += 1
        for i in range(j+2,len(linha)):
            if linha[i] != "\n": lista.append(linha[i])
        lista = ''.join(lista)
        listaDestino.append(lista)

for i in range(len(listaOrigem)): print listaOrigem[i]
for i in range(len(listaDestino)): print listaDestino[i]
I don't know what is going on with the listaDestino, once the listaOrigem is working pretty well.
Please help me to fix it using the following in a txt file as nome:

A -> X

B -> Y
In the first loop you have finished the file, so any additional reads return always the empty string.

If you want to parse the file again add:
import os
arqIn.seek(0, os.SEEK_SET)
before the listaDestino loop.

Also take a look to the syntax to process a file line by line:
for line in arqIn:
    # Do things with the line
    ...
It might make your code much easier!
Wow, dude!
Thank you very much.
Truly appreciate it!