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 6548747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:00:27+00:00 2026-05-25T12:00:27+00:00

I think I am constantly improving my previous question. Basically, I would need to

  • 0

I think I am constantly improving my previous question. Basically, I would need to chunk up a large text (csv) file to send pieces to a multiprocess.Pool. To do so, I think I need at iterable object where the lines can be iterated over.
(see how to multiprocess large text files in python?)

Now I realized that the file object itself (or an _io.TextIOWrapper type) after you open a textfile is iterable line by line, so perhaps my chunking code (now below, sorry for missing it previously) could chunk it, if it could get its length? But if it’s iterable, why can’t I simply call its length (by lines, not bytes)?

Thanks!

def chunks(l,n):
    """Divide a list of nodes `l` in `n` chunks"""
    l_c = iter(l)
    while 1:
        x = tuple(itertools.islice(l_c,n))
        if not x:
            return
        yield x
  • 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-25T12:00:28+00:00Added an answer on May 25, 2026 at 12:00 pm

    The reason files are iterable, is that they are read in series. The length of a file, in lines, can’t be calculated unless the whole file is processed. (The file’s length in bytes is no indicator to how many lines it has.)

    The problem is that, if the file were Gigabytes long, you might not want to read it twice if it could be helped.

    That is why it is better to not know the length; that is why one should deal with data files as an Iterable rather than a collection/vector/array that has a length.

    Your chunking code should be able to deal directly with the file object itself, without knowing its length.

    However if you wanted to know the number of lines before processing fully, your 2 options are

    1. buffer the whole file into an array of lines first, then pass these lines to your chunker
    2. read it twice over, the first time discarding all the data, just recording the lines
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think the file that is produced is an .asm file, any idea how
Im trying to grab all data(text) coming from a URL which is constantly sending
since concurrent programming becomes constantly more important, I was wondering what you think about
Scenario: Think you have got 90TB of text in 200 tables. This is structured
I want to allow my app to send GPS data to server constantly in
I am making some changes on the live site and i constantly need to
I've just migrated from Access where I would write SQL constantly and quickly using
Ok, I think this question is at the wrong place and I'll head over
Think about the following: Your ISP offers you a dynamic ip-address (for example 123.123.123.123).
Think: tiling my emacs window with eshells, a la xmonad. Is this possible? I

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.