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

  • Home
  • SEARCH
  • 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 7774107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:17:43+00:00 2026-06-01T17:17:43+00:00

SO I am reading a text file which has 2 columnn foo, bar SO

  • 0

SO I am reading a text file which has 2 columnn

 foo, bar

SO i did something like

for each_line in f:
   each_line = each_line.split(',')
   foo = int(each_line[0])
   bar = int(each_line[1]
   foobar = FooBar(foo,bar)
   foobar_list.append(foobar)

Now, sometimes either foo or bar is blank.. hence cant be typecasted to int..
Is there a way that if something where foo or bar is empty I can just skip this feature

(doesnt get appended to foodbar) but then the loops stills keeps on going ??

  • 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-01T17:17:45+00:00Added an answer on June 1, 2026 at 5:17 pm

    There are a couple of places this could possibly fail, either if each_line doesn’t have enough commas, or if either value was not a number (say, an empty string). In each case, you can catch the error and use continue to skip to the next item in f:

    for each_line in f:
       try:
           foo_line, bar_line = each_line.split(',')
       except ValueError:
           # "Too many/few values to unpack" meaning wrong number of commas!
           continue
       try:
           foo, bar = int(foo_line), int(bar_line)
       except ValueError:
           # "invalid literal for int()" meaning it wasn't digits
           continue
       foobar = FooBar(foo,bar)
       foobar_list.append(foobar)
    

    I’ve broken this down into two, separate exception handlers because there are two different ways for it to fail. You actually could fold it up like:

    for each_line in f:
       try:
           foo_line, bar_line = each_line.split(',')
           foo, bar = int(foo_line), int(bar_line)
       except ValueError:
           # there was invalid input.
           continue
       foobar = FooBar(foo,bar)
       foobar_list.append(foobar)
    

    Since it’s the same exception and they happen close together. Personally, i prefer the former to make it really clear that there are two kinds of errors. What you should NOT do is:

    # !!! BAD !!!
    for each_line in f:
        try:
            each_line = each_line.split(',')
            foo = int(each_line[0])
            bar = int(each_line[1]
            foobar = FooBar(foo,bar)
            foobar_list.append(foobar)
        except ValueError:
            continue
    

    because FooBar() or even foobar_list.append() could fail, but the exception handler can swallow it; Always make the try: suite in yoru exception handlers as small as possible so that they only catch one error, and where the error is is easy to find.

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

Sidebar

Related Questions

I have a text file with data which might look something like: UserName Address
So I am reading from a text file which has product information I have
I am reading a text file which contains numbers in the range [1, 10^100].
I am reading a text file which I guess is encoded in utf-8. Some
I have a text file that I am reading. It has chunks that I
I have a text file which has first line as below: j0W82LBrSdUbw Basically it
I want to read a text file which has text as follows(sample) abc ip-32-56-198-18.com
I have a text file which is encoded with codepage 850. I am reading
I'm reading a text file via CGI in, in perl, and noticing that when
I have some code reading a text file and scanning for words between brackets:

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.