简体   繁体   English

ValueError:int()以10为底的无效文字:''

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

I got this error in Python. 我在Python中遇到此错误。

I am reading from a text file, put the value in a variable and try to convert into Integer. 我正在从文本文件中读取,将值放在变量中,然后尝试转换为Integer。 This is my code 这是我的代码

fo = open("HCTC3152_INF.TXT", "r")
line1 = fo.readline()

the part in between 52:56 is the year so is always like 2013 or 2014 52:56之间的部分是年份,所以总是像2013或2014

ChangeOverStartDateYYYY= int(line2[52:56],10)
print ChangeOverStartDateYYYY

so then, print always the years in the command prompt, and at the end appears that error. 因此,请始终在命令提示符下打印年份,最后出现该错误。

You got an empty line, or a at the very least a line shorter than 52 characters. 您有一个空行,或者至少有一个于52个字符的行。

Skip those lines: 跳过这些行:

if not line.strip():

to skip empty lines (consisting only of whitespace), or test for a minimal length: 跳过空行(仅由空白组成),或测试最小长度:

if len(line) >= 56:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM