2013-08-04 37 views
0

我在我的代码中看到了一个列表,它看起来像。 L = ['Nickname', '35'] 当我尝试i = int(L[2])它捕获异常无法将列表中的元素转换为整数

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

我在做什么错?

 namesplitted = line.split() 
     lnum += 1 
     truename = namesplitted[0] 
     kills = namesplitted[1] 
     print kills 
     >>> 34 
     i = int(kills[1]) 
+1

列表索引基于0。尝试'L [1]'。 –

+0

我的不好,其实L [1] – Syberic

+0

你能否提供更多的代码,并仔细检查你使用的样本数据?在我的机器上试用这个(Python 3.3.2),这似乎适用于您提供的数据和示例。 –

回答

2

这是因为你的号码'35'位于L[1]。列表索引从Python中的0开始。所以第一个元素是L[0],第二个元素是L[1]等等。

你的名单可能是L = ['Nickname', '35', '']

>>> L = ['Nickname', '35', ''] 
>>> int(L[2]) 

Traceback (most recent call last): 
    File "<pyshell#142>", line 1, in <module> 
    int(L[2]) 
ValueError: invalid literal for int() with base 10: '' 
>>> int(L[1]) 
35 
+0

不,在我的代码中它实际上是L [1],而且我仍然有错误 – Syberic

+0

您确定在问题中给出的列表是您的实际列表吗? –

+0

发表我的实际代码 – Syberic

相关问题