ValueError: invalid literal for int() with base 10: ''

前端 未结 18 1904
甜味超标
甜味超标 2020-11-22 02:06

I am creating a program that reads a file and if the first line of the file is not blank, it reads the next four lines. Calculations are performed on those lines and then t

相关标签:
18条回答
  • 2020-11-22 02:48

    I got into the same issue when trying to use readlines() inside for loop for same file object... My suspicion is firing readling() inside readline() for same file object caused this error.

    Best solution can be use seek(0) to reset file pointer or Handle condition with setting some flag then create new object for same file by checking set condition....

    0 讨论(0)
  • 2020-11-22 02:51

    So if you have

    floatInString = '5.0'
    

    You can convert it to int with floatInInt = int(float(floatInString))

    0 讨论(0)
  • 2020-11-22 02:53

    You've got a problem with this line:

    while file_to_read != " ":
    

    This does not find an empty string. It finds a string consisting of one space. Presumably this is not what you are looking for.

    Listen to everyone else's advice. This is not very idiomatic python code, and would be much clearer if you iterate over the file directly, but I think this problem is worth noting as well.

    0 讨论(0)
  • 2020-11-22 02:54

    Just for the record:

    >>> int('55063.000000')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: invalid literal for int() with base 10: '55063.000000'
    

    Got me here...

    >>> int(float('55063.000000'))
    55063.0
    

    Has to be used!

    0 讨论(0)
  • 2020-11-22 02:56

    Pythonic way of iterating over a file and converting to int:

    for line in open(fname):
       if line.strip():           # line contains eol character(s)
           n = int(line)          # assuming single integer on each line
    

    What you're trying to do is slightly more complicated, but still not straight-forward:

    h = open(fname)
    for line in h:
        if line.strip():
            [int(next(h).strip()) for _ in range(4)]     # list of integers
    

    This way it processes 5 lines at the time. Use h.next() instead of next(h) prior to Python 2.6.

    The reason you had ValueError is because int cannot convert an empty string to the integer. In this case you'd need to either check the content of the string before conversion, or except an error:

    try:
       int('')
    except ValueError:
       pass      # or whatever
    
    0 讨论(0)
  • 2020-11-22 02:56

    I recently came across a case where none of these answers worked. I encountered CSV data where there were null bytes mixed in with the data, and those null bytes did not get stripped. So, my numeric string, after stripping, consisted of bytes like this:

    \x00\x31\x00\x0d\x00
    

    To counter this, I did:

    countStr = fields[3].replace('\x00', '').strip()
    count = int(countStr)
    

    ...where fields is a list of csv values resulting from splitting the line.

    0 讨论(0)
提交回复
热议问题