TransWikia.com

FionaDeprecationWarning: Collection.__next__() is buggy and will be removed in Fiona 2.0. Switch to `next(iter(collection))`. record = next(c)

Geographic Information Systems Asked by ASharma on May 11, 2021

I have some Python3 code used to import shapefiles using fiona. This is the main set of lines:

I created some code (Python3) with these lines:

c = fiona.open(dataDir + "/" + sourceFile, encoding=shapefileEncoding)
for i in range (0, len(c)):
                        # Get next record
                        record = next(c)
                        .......
c.closed

The code works perfectly but the output includes this warning:

FionaDeprecationWarning: Collection.__next__() is buggy and will be removed in Fiona 2.0. Switch to `next(iter(collection))`.  record = next(c)

I probably don’t understand what iterable iter refers to. I replaced next(c) in my code with next(iter(c)) and my code no longer works: I get the same record repeatedly instead of iterating through all records.

What should I replace the next(c) in my code with?

One Answer

You can just loop over the collection:

with fiona.open(dataDir + "/" + sourceFile, encoding=shapefileEncoding) as c:
    for record in c:
        do something with record

Using a context manager (the with statement) removes the need for closing the dataset explicitly.

If you need i (the record index) for anything, you can enumerate:

with fiona.open(dataDir + "/" + sourceFile, encoding=shapefileEncoding) as c:
    for i, record in enumerate(c):
        do something with record

Correct answer by user2856 on May 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP