Python Forum
Simple script writted by a dumb dude, myself
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple script writted by a dumb dude, myself
#1
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
Reply
#2
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!
Reply
#3
Wow, dude!
Thank you very much.
Truly appreciate it!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I dumb? aidh18 2 1,086 Mar-22-2022, 10:16 PM
Last Post: deanhystad
  Simple Python script, path not defined dubinaone 3 2,692 Nov-06-2021, 07:36 PM
Last Post: snippsat
  Dumb newbie question JonEdward 5 3,234 Jul-22-2020, 10:06 PM
Last Post: snippsat
  Need help creating a simple script Nonameface 12 4,526 Jul-14-2020, 02:10 PM
Last Post: BitPythoner
  Simple text to binary python script gmills13 2 2,809 Feb-04-2020, 08:44 PM
Last Post: snippsat
  Made a simple script for android (really simple) but it is not running anddontyoucomebacknomore 2 2,357 Mar-06-2019, 12:19 AM
Last Post: anddontyoucomebacknomore
  Simple script that seems to misbehave? Nwb 1 2,333 Jun-10-2018, 05:30 AM
Last Post: Nwb
  First time with Python.. need help with simple script shakir_abdul_ahad 7 5,444 May-06-2018, 09:28 AM
Last Post: killerrex
  help with a simple script juanb007 4 3,602 May-01-2018, 08:06 PM
Last Post: ThiefOfTime
  Need help with a simple AHK script Stabu 0 2,088 Feb-24-2018, 08:27 PM
Last Post: Stabu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020