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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:35:50+00:00 2026-06-08T17:35:50+00:00

This is sort of two coding style questions in one. I have a tab-dl’d

  • 0

This is sort of two coding style questions in one. I have a tab-dl’d file where ‘objects’ are separated by a blank line. The first line of the ‘object’ is the ID. The remaining lines up until a blank line are things that belong to the object. I want to parse this into a hash as below:

f = open(someFile, 'rb')
c = csv.reader(f, delimiter = "\t", quoting = csv.QUOTE_NONE)
thingstore = {}

try:
    for row in c:
        title = row[0]
        thingstore[title] = set()
        item = map(fixStupidExcelCrap, c.next())
        while ''.join(item).strip() != '':
            thingstore[title].add(tuple(item))
            item = map(fixStupidExcelCrap, c.next())
except StopIteration:
    pass

f.close()

There are a couple things I think are ugly about this solution. Firstly, having the try block surrounding the entire function seems like asking for problems as improperly formatted files may not be detected. One alternative is wrapping each next() call in a try block and setting a flag to exit the outer loop which also seems hacky.

Secondly, while ''.join(item).strip() != '': is very ugly. Is there a better way to test for an empty line that’s been parsed by the csv module?

Update:

I missed a detail that affects the test for an empty line. As you might have guessed, the code is parsing a tab delimited file exported from Excel. The fun thing about empty lines in this scenario is that they’re not really empty – all lines in the file have the same number of tabs. So if you have 3 columns in the excel file, an empty line in the exported tab delimited file will have 2 tabs in it and csv will parse that into ['', '', ''] which bool evaluates to True.

So wrt Ignacio’s much prettier answer, for row in itertools.takewhile(bool, c): won’t work because it’ll snarf up the rest of the file, empty lines and ID rows included. for row in itertools.takewhile(lambda x: ''.join(x).strip() != '', c): does work, but we’re back to the ugliness I was trying to avoid (the strip() probably isn’t necessary, but I put it in to be on the safe side).

  • 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-06-08T17:35:51+00:00Added an answer on June 8, 2026 at 5:35 pm

    Blech. Empty lines result in an empty list.

    with open(...) as fp:
      c = csv.reader(fp)
      while True:
        try:
          title = next(c)[0]
          obj = set()
          store[title] = obj
        except StopIteration:
          break
        for row in itertools.takewhile(bool, c):
          obj.add(tuple(row))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to sort a two-dimensional array by a timestamp column Descending. This
how to sort a list in Scala by two fields, in this example I
I have this sort of format asp.net MVC View -> Service Layer -> Repository.
The UNIX sort command can sort a very large file like this: sort large_file
I have been working on this sort of ATM (With a maximum of 50
I'm coding a script that takes one csv file to transform it into another
Background: I have two web pages, one is a list of things, and the
This is my first time doing this sort of project so apologies if the
I know that this sort of question has been asked here before, but still
I am mocking in this sort of situation: class A { public IB B

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.