Python导入Excel列表



我在Python中有以下列表:

list = [[1, (200, 45)], [2, (53, 543)], [3, (5, 5)], [4,(655, 6456464)],[5, (64564, 45)], [6, (6, 5445)], [7, (546, 46)], [8, (64, 645)]

我现在想将这些保存到Excel中,然后在其中读取。如何做到这一点?我还没有找到这样一个";简单的";使用谷歌导入,主要是导入更复杂的Excel文件。

感谢

Serpiente32

下面是一个示例代码:

import pandas as pd
list_ = [[1, (200, 45)], [2, (53, 543)], [3, (5, 5)], [4,(655, 6456464)],[5, (64564, 45)], [6, (6, 5445)], [7, (546, 46)], [8, (64, 645)]]
list_ = [[item[0], *item[1]] for item in list_]
# The line above just unpacks the tuple and makes every element a list of 3 numbers
pd.DataFrame(data=list_).to_excel("data.xlsx", index=False) # save in excel
# you now have an excel file in same folder with three columns
data = pd.read_excel("data.xlsx")  # read back from excel
print(data)

输出:

0      1        2
0  1    200       45
1  2     53      543
2  3      5        5
3  4    655  6456464
4  5  64564       45
5  6      6     5445
6  7    546       46
7  8     64      645

注意:永远不要使用像list这样的保留关键字作为标签名称。可能会在程序中引发意外问题。

最新更新