Problem with Python string formatting

Asked

Viewed 97 times

0

First I would like to inform you that: (a) I am an accountant and I program only to help me with tasks with a large mass of data, so my code below will not be a professional code, and (b) this is my first question here, then if information is missing that is important for the answer I apologize.

Come on

I’m doing a program in Python to help me correct a digital bookkeeping file - ECD. and after making the necessary corrections (which were archived in an array (list list)) called I155reduced, I want to export to a file called ECD Fixed.

At the moment I am writing in the file the corrected record appears the following error message:

Traceback (Most recent call last): File "fix ECD_v2.py", line 288, in I155reduced[int(i)][9] + '| n') Valueerror: Unknown format code 'f' for Object of type 'str'

the code that generates this error is:

276    i = 0
277    with open(arquivoECD, encoding="utf8") as ECD:
278        for linha in ECD:
279            if str(I155Reduzido[int(i)][0]) == str(contadorRegistro):
280                registroCorrigido = ('|I155|' +
281                                     I155Reduzido[int(i)][1] + '|' +
282                                     I155Reduzido[int(i)][2] + '|' +
283                                     '{:.2f}'.format(str(I155Reduzido[int(i)][4]).replace('.', ',')) + '|' +
284                                     I155Reduzido[int(i)][5] + '|' +
285                                     '{:.2f}'.format(str(I155Reduzido[int(i)][12]).replace('.', ',')) + '|' +
286                                     '{:.2f}'.format(str(I155Reduzido[int(i)][14]).replace('.', ',')) + '|' +
287                                     '{:.2f}'.format(str(I155Reduzido[int(i)][8]).replace('.', ',')) + '|' +
288                                     I155Reduzido[int(i)][9] + '|\n')
289                ECD_corrigido.writelines(registroCorrigido)
290                if i < (len(I155Reduzido)-1):
291                    i = i + 1
292
293            else:
294                ECD_corrigido.writelines(linha)
295            contadorRegistro = contadorRegistro + 1

Question: what am I doing wrong on the line at 288, if line 284 has the same character? what am I missing?

I am using Ubuntu 19.10 and python3.7

I thank you in advance for the aid

  • 1

    What was the purpose of formatting a string with the format {:.2f}? This format is for decimal numbers.

  • Woss, I take the following line from a TXT file |I155|2110310001||1234,56|D|12,34|1,23|1234,56|D|, I need to sum up the values of this line, and python works with "." for the decimals, but the layout that the federal revenue stipulated and with "," to decimals. Then I convert the comma to point at the input and at the time of exporting the values (which is the code snippet) I have to convert from semicolon to comma again. Hence the conversion of some fields

2 answers

0

Change the parses from str to float:

Of

str(I155Reduzido[int(i)][4]).replace('.', ','))

To

float(I155Reduzido[int(i)][4]).replace('.', ','))

0


Try using locale to use decimal separation.

import locale

locale.setLocale(locale.LC_ALL, '')

Then in number formatting you will not need to replace. For example:

valor = 9.93 # Decimal em python
print('{:n}'.format(valor)) # resultado: 9,93

I hope this can help.

Browser other questions tagged

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