The problem was basically due to an inconsistent call to the function sum
:
sum([dados[2:17] for dados in dados])
If the documentation:
sum(iterable[, start])
Sums start
and the items of an iterable
from left to right and Returns the total. start
defaults to 0. The iterable’s items are normally Numbers, and the start
value is not allowed to be a string.
Where it is possible to check that the default value of start
is 0. You have a list of lists, something like [['1', '2'], ['3', '4']]
, then in the first iteration within the function sum
the operation equivalent to result = start + ['1', '2']
; as start
is 0, basically you will be adding a int
with a list
, giving the cited error.
A simple way around this would be to set the initial value, start
, as an empty list, thus the operation equivalent to result = [] + ['1', '2']
. To do this, just define the second argument value of the function sum
:
sum([dados[2:17] for dados in dados], [])
Some caveats about your code:
In doing dados = list(linhas)
you will store the entire contents of the file in memory. If the read file is too large it may lock the application. For each row of the file an object will be created list
and for each item in the list an object str
, which would require even more memory than the file size itself;
The line map(int, dados)
has no use in the code the way it is. What function map
makes returns an iterator that applies the defined function on the elements of the informed iterable, in this case applies int
in the items of dados
. The function does not modify the parameters, so if you do not assign the return of the function in any variable it will be in vain in the code;
To bypass the first one, you can work directly on the iterators, without loading everything in memory. For example, linhas
will be an eternal with the lines of your file, as you just want the lines between 1730 and 1830, just you take that part of the eternal:
linhas_que_voce_quer = itertools.islice(linhas, 1730, 1830)
This will return another everlasting, but only to the lines you need. All others will not be loaded in memory (not at the same time, at least). From it you can use the function map
to convert to integer and function sum
to join, or use the function itertools.chain.from_iterable
, that also makes this unification.
map(int, dados)
, this line of code is doing nothing. It does not modifydados
, it has a return that needs to be assigned to a variable.– Woss