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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:48:59+00:00 2026-05-21T16:48:59+00:00

I have a small file read routine and I want only the 1st 200

  • 0

I have a small file read routine and I want only the 1st 200 records I have it working but along the way I could not figure out what was wrong with using the “while” construct.
This code works:

import csv, sys, zipfile
sys.argv[0] = "/home/tom/Documents/REdata/AllListing1RES.zip"
zip_file    = zipfile.ZipFile(sys.argv[0])
items_file  = zip_file.open('AllListing1RES.txt', 'rU')
rows = []
for row_index, row in enumerate(csv.DictReader(items_file, dialect='excel', delimiter='\t')):
    if (row_index < 200):
        rows.append(row)
    else : break

This code runs until it fails with an out of memory condition I would have thought it was equivalent?

import csv, sys, zipfile
sys.argv[0] = "/home/tom/Documents/REdata/AllListing1RES.zip"
zip_file    = zipfile.ZipFile(sys.argv[0])
items_file  = zip_file.open('AllListing1RES.txt', 'rU')
rows = []
for row_index, row in enumerate(csv.DictReader(items_file, dialect='excel', delimiter='\t')):
    while (row_index < 200):
        rows.append(row)
    else : break

so what would be the right construct using while? –

  • 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-21T16:49:00+00:00Added an answer on May 21, 2026 at 4:49 pm

    The more traditional way of writing that loop would be:

    for row_index, row in enumerate(csv.DictReader(items_file, dialect='excel', delimiter='\t')):
        if (row_index >= 200):
            break
        rows.append(row)
    

    As soon as the row counter hits 200, we bail out of the loop.

    To use a while loop instead of a for loop (note that, as a looping construct, while is an alternative to for rather than to if) it is necessary to step through the iterator manually:

    itr = enumerate(csv.DictReader(items_file, dialect='excel', delimiter='\t'))
    row_index = -1
    while row_index < 199:
        try:
            row_index, row = next(itr) # Python 3. Use itr.next() in Python 2
        except StopIteration:
            break # Ran out of data
        rows.append(row)
    

    All that said, there’s actually a superior alternative to both of these options available in the itertools module:

    from itertools import islice
    itr = csv.DictReader(items_file, dialect='excel', delimiter='\t')
    rows = list(islice(itr, 200))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small script we're using to read in a CSV file containing
Are there any simple way to read all text from a small text file
can anyone help? I have small procedure to read in an UTF-8 file with
I have a small text file that I'd like to read into a scalar
I'm writing a small PE file analyzer and I have to read the contents
I have a small file test.txt contains a Long number inside while another piece
I have a small Java file interacting with a postgresql database, so I've downloaded
I have a small php file which just creates the tables in a database.
I have a program that creates a small file in the Bin directory for
I have a small job that takes a text file of email/zip codes and

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.