Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3980702
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:19:21+00:00 2026-05-20T05:19:21+00:00

In a basic I had the next process. import csv reader = csv.reader(open(‘huge_file.csv’, ‘rb’))

  • 0

In a basic I had the next process.

import csv
reader = csv.reader(open('huge_file.csv', 'rb'))

for line in reader:
    process_line(line)

See this related question. I want to send the process line every 100 rows, to implement batch sharding.

The problem about implementing the related answer is that csv object is unsubscriptable and can not use len.

>>> import csv
>>> reader = csv.reader(open('dataimport/tests/financial_sample.csv', 'rb'))
>>> len(reader)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type '_csv.reader' has no len()
>>> reader[10:]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '_csv.reader' object is unsubscriptable
>>> reader[10]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '_csv.reader' object is unsubscriptable

How can I solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:19:21+00:00Added an answer on May 20, 2026 at 5:19 am

    Just make your reader subscriptable by wrapping it into a list. Obviously this will break on really large files (see alternatives in the Updates below):

    >>> reader = csv.reader(open('big.csv', 'rb'))
    >>> lines = list(reader)
    >>> print lines[:100]
    ...
    

    Further reading: How do you split a list into evenly sized chunks in Python?


    Update 1 (list version): Another possible way would just process each chuck, as it arrives while iterating over the lines:

    #!/usr/bin/env python
    
    import csv
    reader = csv.reader(open('4956984.csv', 'rb'))
    
    chunk, chunksize = [], 100
    
    def process_chunk(chuck):
        print len(chuck)
        # do something useful ...
    
    for i, line in enumerate(reader):
        if (i % chunksize == 0 and i > 0):
            process_chunk(chunk)
            del chunk[:]  # or: chunk = []
        chunk.append(line)
    
    # process the remainder
    process_chunk(chunk)
    

    Update 2 (generator version): I haven’t benchmarked it, but maybe you can increase performance by using a chunk generator:

    #!/usr/bin/env python
    
    import csv
    reader = csv.reader(open('4956984.csv', 'rb'))
    
    def gen_chunks(reader, chunksize=100):
        """ 
        Chunk generator. Take a CSV `reader` and yield
        `chunksize` sized slices. 
        """
        chunk = []
        for i, line in enumerate(reader):
            if (i % chunksize == 0 and i > 0):
                yield chunk
                del chunk[:]  # or: chunk = []
            chunk.append(line)
        yield chunk
    
    for chunk in gen_chunks(reader):
        print chunk # process chunk
    
    # test gen_chunk on some dummy sequence:
    for chunk in gen_chunks(range(10), chunksize=3):
        print chunk # process chunk
    
    # => yields
    # [0, 1, 2]
    # [3, 4, 5]
    # [6, 7, 8]
    # [9]
    

    There is a minor gotcha, as @totalhack points out:

    Be aware that this yields the same object over and over with different contents. This works fine if you plan on doing everything you need to with the chunk between each iteration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i thought i had solved this but my basic example seems to not be
I just had some basic php questions to further my understanding as I learn,
I have a basic cs-major understanding of multi-threading but have never had to do
The basic problem is like this: A subscriber has successfully replicated a row from
I had a huge file for creating this GUI and I have shortened it
Basic requests are: human readable / text format (for easy version control) online (for
Basic question : How to I create a bidirectional one-to-many map in Fluent NHibernate?
Basic premise: I have a Room which publishes an event when an Avatar enters
My basic question is, in .NET, how do I clone WebControls? I would like
Very basic question: how do I write a short literal in C++? I know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.