1
I have a file with several lines and I want to add a certain range considering all lines. To simplify, I took as an example:
AAA0000011111000090011
BBB0000011111000080011
CCC0000011111000070011
There are several more lines and fields too, but I know the interval I want to add. If you want to add up "00009", "00008" and "00007" which start in heading 14 and end in heading 18, what is the most efficient way? I also want to add "0011", "0011" and "0011". I want to write the fields in a new file.
The result would be a new file with the sums:
24
33
I thought as follows:
file_name = teste.GS3
arquivo = open(file_name, "r")
TOTAL_CHARGEABLE_UNITS = 0
DATA_VOLUME_OUTGOING = 0
i = True
for line in arquivo:
if i: # para pular a primeira linha
i = False
continue
TOTAL_CHARGEABLE_UNITS = TOTAL_CHARGEABLE_UNITS + sum(line[14:18] + ...)
DATA_VOLUME_OUTGOING = DATA_VOLUME_OUTGOING + sum(line[19:22] + ...)
arquivo.close()
arquivo = open("/dados/cdrs-roaming/resultado.txt", "w")
arquivo.writelines([TOTAL_CHARGEABLE_UNITS],[DATA_VOLUME_OUTGOING])
arquivo.close
TOTAL_CHARGEABLE_UNITS
and DATA_VOLUME_OUTGOING
are the desired fields in this case. I am beginner so I had no idea how to create this sum. Any idea?
Detail: I believe I should convert these fields/ranges to float
, but don’t know how to do this without converting the entire file.
A business that can help you is, from each line, create a high level object. There, with a list of these objects you operate on their properties. It’s not the best option if it’s something you need to do once and never look again, but if this data is the basis of a larger project, then it might be worth Take a look at this answer: https://answall.com/questions/399778/como-extrair-as-informa%C3%A7%C3%b5es-de-um-file-cnab-using-python/400033#400033
– jsbueno