我使用xlrd导入Excel工作表并将其存储到2d列表中( data [row] [col] ),我希望将列表中的每个元素拆分为' \ N'#39 ;.现在我只是在for循环中将它存储到另一个变量中,但是可以将列表从2d扩展到3d( data [row] [col] [line] )?如果是,怎么样?
sh = book.sheet_by_index(0)
data = [[sh.cell_value(r, c) for c in range(sh.ncols)] for r in range(sh.nrows)]
for row in range(sh.nrows):
for col in range (sh.ncols):
data_lines=data[row][col].split("\n")
*do stuff with variable*
我会从扩展列表中受益,因为我可以随时调用我需要的行,而无需迭代它
答案 0 :(得分:2)
您可以在每个元素上调用split
:
li = [['a b c', 'd e f'], ['g h i', 'j k l']]
for index, line in enumerate(li):
for inner_index, col in enumerate(line):
li[index][inner_index] = li[index][inner_index].split()
print(li)
# [[['a', 'b', 'c'], ['d', 'e', 'f']], [['g', 'h', 'i'], ['j', 'k', 'l']]]