Check whether the number is positive, negative or zero

Asked

Viewed 2,565 times

2

Hello. I am studying and learning Python, and at the right moment makes a code that reads a column of a given file where values vary between positive, negative and zero, and that writes an output file (.txt) where the answer is -1 for negative, 0 for null and 1 for positive.

An example of file values:

   0; 0.0000
   1;-0.5921
   2;-0.3673
   3; 0.0000
   4; 0.0235
   5; 0.1685

The output file would look something like this:

   0; 0
   1; -1
   2; -1
   3; 0
   4; 1
   5; 1

The code I’ve written so far is:


arquivo = open('vazdif.out', 'rt')
i = 0

for linha in arquivo:
    campo = linha.split(';')
    vaz = float(campo[1])
    i = i+1
    fou = open('res.id.txt','wt')
pass
def vazdiff():
    if vaz < 0:
        if vaz == 0:
            vazdiff = 0
        else:
            vazdiff = 1 
    else:
        vazdiff = -1
    return vazdiff
pass
fou.write('{};{}'.format(i,vazdiff()))
fou.close

But I’m having difficulties to finish it and make the output file come out correct, and also to make it as compact as possible.

Any help and explanation about the code will be welcome, thank you.

1 answer

2


Pay attention:

  • In this code of yours, there are these two pass, who are effectively doing nothing;

  • The line fou.write('{};{}'.format(i,vazdiff())) is not inside a for, so save only 1 element in the new txt;

  • The method vazdiff(), as it is done, calculates the result only of the last vaz found;

  • Also, the logic within it is wrong. Note that the code will never enter into if vaz == 0:, for this if is inside if vaz < 0:. That is, if vazis less than 0, it cannot be == 0

  • Use \n to break the line inside the new txt;

  • Create a list to save results.

Try something like this:

arquivo = open('vazdif.out', 'rt')
lista = []

# Leia o arquivo
for linha in arquivo:
    campo = linha.split(';')
    #print(campo)
    vaz = float(campo[1])
    print(vaz)
    # Salve os dados na lista
    if vaz < 0:
        lista.append("-1")
    elif vaz == 0:
        lista.append("0")
    elif vaz > 0:
        lista.append("1")
arquivo.close()
print(lista)

fou = open('res.id.txt','wt')
i = 0
#Percorra a lista e salve o resultado em outro arquivo
for l in lista:
    fou.write('{}; {}\n'.format(i,l))
    i+=1
fou.close()

If you want to use the method vazdiff(), pass a parameter to it:

arquivo = open('vazdif.out', 'rt')
lista = []

def vazdiff(vaz):
    if vaz < 0:
        lista.append("-1")
    elif vaz == 0:
        lista.append("0")
    elif vaz > 0:
        lista.append("1")

# Leia o arquivo
for linha in arquivo:
    campo = linha.split(';')
    #print(campo)
    vaz = float(campo[1])
    print(vaz)
    # Chame o metodo, passando o parametro
    vazdiff(vaz)

arquivo.close()
print(lista)

fou = open('res.id.txt','wt')
i = 0
#Percorra a lista e salve o resultado em outro arquivo
for l in lista:
    fou.write('{}; {}\n'.format(i,l))
    i+=1
fou.close()

Browser other questions tagged

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