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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:24:15+00:00 2026-06-08T14:24:15+00:00

I read a file, containing a list of numbers, (which are some identifiers that

  • 0

I read a file, containing a list of numbers, (which are some identifiers that I need later).

If there is an empty line at the end of my file, I have a bug in the following code:

return [ int(x)  for x in lines if not x == '' and not x == "\r\n"]

with the following python output:

  [...]
File "Z:\Projects\PyIntegrate\perforceIntegration.py", line 453, in readChange
ListNumbers
    self.changeListNumbers = loadChangeListNumbers()
  File "Z:\Projects\PyIntegrate\perforceIntegration.py", line 88, in loadChangeL
istNumbers
    return [ int(x)  for x in lines if not x == '' and not x == "\r\n"]
ValueError: invalid literal for int() with base 10: ''

obviously my test if not x == '' and not x == "\r\n" is not sufficient for handling this situation.

What do I do wrong ?

(everything is ok if I suppress the last empty line of my file, i.e. if i let the last line of my file containing really a number)

  • 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-08T14:24:17+00:00Added an answer on June 8, 2026 at 2:24 pm

    Try this:

    return [ int(line) for line in lines if line.strip() ]
    

    This will strip all spaces and newlines from the line and if it is not empty (i.e. it contains some characters, preferably numbers), it will convert it into int.

    It will however fail (and raise ValueError) if your file contains other characters than digits or it contains spaces between digits.

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

Sidebar

Related Questions

Say that I call select() on a FD_SET containing a bunch of read file
I am trying to read a comma separated file through InputStream containing some Slovak
I am trying to read file and split its line to get some context(Computer
I'm writing a script that has two inputs: a file containing a list of
i have to write a program in C to read a file containing several
I read file, but in the end of file i get unknown symbols: int
I have a small script we're using to read in a CSV file containing
I want to read a text file containing space separated values. Values are integers.
I'm trying to store a file which encode an id of a class, read
i have a file containing list paths and other related information of all the

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.