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.