我在一个文件中有数据,该文件有两组值,然后是一组未指定的数组(每个数组中有 3 个子项)
例如:
('January', 2, [('curly', 30), ('larry',10), ('moe',20)])
我需要读取数据并返回并将部分数据重新分配给新变量。
例如:
Month: January
Section: 3
curly has worked 30 hours
larry has worked 10 hours
moe has worked 20 hours
我可以读取字符串的前两部分,但不知道如何分解数组- 每个文件可能有不同数量的子数组,所以需要像 while 循环一样吗?
import ast
filecontent = ast.literal_eval(filename.read())
for item in filecontent:
month = filecontent[0]
section = filecontent[1]
name1 = filecontent[2] # not working
hours1 = filecontent[3]# not working
name2 = filecontent[4]# not working
hours2 = filecontent[5]# not working
# account for additional arrays somehow?
print ("month:" + month)
print ("section" + str (section))
print (str (name1) + "has worked" + str (hours1))
print (str (name2) + "has worked" + str (hours2))