How to Ignore \n when reading in strings?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Jones
    New Member
    • Jan 2011
    • 1

    #1

    How to Ignore \n when reading in strings?

    Hi,

    I am new to using python. I have a problem. I need to read in strings from a data file and only count those that are explicitly numbers. So I am using the isdigit() method.

    But the problem is that the data file has multiple strings so the problem is the \n messes my code up.

    My program only works on the last number because that is the end of the file and there are no more empty spaces or a return at the end of file. So it only counts the last number as being a number.

    Is there anyway to ignore \n when reading in strings or does readline() and isdigit() automatically ignore this \n?

    Thanks!
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Use string method strip() to remove leading and trailing whitespace characters such as "\n", "\t", etc.

    Comment

    Working...