Simple script writted by a dumb dude, myself - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Simple script writted by a dumb dude, myself (/thread-9784.html) |
Simple script writted by a dumb dude, myself - mm14ag - Apr-27-2018 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 RE: Simple script writted by a dumb dude, myself - killerrex - Apr-28-2018 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! RE: Simple script writted by a dumb dude, myself - mm14ag - Apr-28-2018 Wow, dude! Thank you very much. Truly appreciate it! |