【发布时间】:2020-03-19 18:50:58
【问题描述】:
作为我代码的一部分,我需要迭代到列表的特定索引(从 csv 文件中获取)并将这些元素的类型从 str 更改为 诠释。但是,当我遍历索引并对其进行转换时,元素不会将其类型更改为 int。
我很困惑为什么以及如何做到这一点?
def generate_order(bom, parts_cost_filename):
myfile = open(parts_cost_filename,'r')
# Retrieving the headers for each of the files part name and the price
header = csv.reader(myfile)
header_list = []
for line in header:
header_list += line
break
header_list = list(header_list)
for number in header_list[1:1]:
number = int(number)
print(header_list)
myfile.close()
【问题讨论】:
-
请修正帖子中代码的缩进。
-
header_list已经是您在第 4 行声明的列表,不需要额外的list()转换,而且[1:1]将是一个空列表 -
[1:1] 将是一个空列表。当你迭代它时,你可能无法用它做任何事情。