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

前端 未结 18 1914
甜味超标
甜味超标 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条回答
  •  闹比i
    闹比i (楼主)
    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.

提交回复
热议问题