Python导入Excel列表
我在Python中有以下列表:Python导入Excel列表,python,Python,我在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
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
作为标签名称。可能会导致程序中出现意外问题。您希望如何设置格式?在3列中?是的,那太完美了:)