I am trying to iterate a number of times over an iterable. Howver, the issue is that once the iterable is consumed it no longer can yield. Which means, after my first iteration I no longer can use the iterable.
In my case, I have an excel file of 10 000 lines, I am creating a textfilereader to avoid importing all my data into memory. I am fixing a number of iterations which will be used to execute the same opeartions over the lines of the excel file. Because of the fading nature of the iterable, I can't use the operations after the second iteration. So, I tried to create a global iteraton loop where I define the iterable each time: Is there a better way to get around this issue
The main reason of using an iterable in my case is to avoid loading data in memory .
Code causing issue
### read file through an iterable
df_test = pd.read_csv('filet_to_read.csv',sep=';',quotechar='"', escapechar='\\', iterator=True, chunksize=15, encoding='utf-8',converters={'Ident':str})
### iterations
iterations=5
for iter in range(iterations):
for chunk in df_test:
##Do_operations
print('end of itertaion :',iter)
### After first iteration, no more operations are possible because iterable is consumed
My solution
iterations=5
for i in range(iterations):
df_test = pd.read_csv('filet_to_read.csv',sep=';',quotechar='"', escapechar='\\', iterator=True, chunksize=15, encoding='utf-8',converters={'Ident':str})
for chunk in df_test:
##Do_operations
print('end of itertaion :',iter)
csv
module andseek()
the start of the file at the end? (I actually don't know if seek is a method on the csvreader object, I'll have to look it up)