vin*_*jha -2 python excel dictionary python-3.x openpyxl
下面是我的字典 & 我想将字典的键值对写入 Excel 工作表中名为key&的两列中hourly。
'One year reserved'= {\n 'Australia Central 2': 0.0097,\n 'East US 2': 0.00605,\n 'North Central US': 0.00605,\n 'South Africa West': 0.01016,\n 'UK West': 0.00685,\n 'France South': 0.01119,\n 'Korea': 0.00639,\n 'Canada East': 0.00685,\n 'US Gov Virginia': 0.00879,\n 'East Asia': 0.0097,\n 'South India': 0.01005,\n 'South Central US': 0.00731,\n 'West US': 0.00719,\n 'Australia East': 0.00776,\n 'Canada Central': 0.00674,\n 'Australia Southeast': 0.00776,\n 'Southeast Asia': 0.00776,\n 'Central US': 0.00731,\n 'West India': 0.00833,\n 'East US': 0.00605,\n 'Australia Central': 0.0097,\n 'UK South': 0.00685,\n 'Japan East': 0.00799,\n 'Japan West': 0.00879,\n 'West Europe': 0.00696,\n 'Brazil South': 0.00982,\n 'Korea Central': 0.00799,\n 'US Gov Texas': 0.00879,\n 'US Gov Arizona': 0.00879,\n 'Central India': 0.00833,\n 'North Europe': 0.00822,\n 'West Central US': 0.00731,\n 'France Central': 0.00856,\n 'South Africa North': 0.00811,\n 'West US 2': 0.00605\n }\nRun Code Online (Sandbox Code Playgroud)\n\n使用 python openpyxl 库将字典转换为 xls 文件。\n输出应如下所示:-
\n\n**Key** **Hourly**\nAustralia Central 2 0.008\nEast US 2 0.00605\nNorth Central US\xc2\xa0 0.00605\nRun Code Online (Sandbox Code Playgroud)\n
这是一个使用 python 3.6+ 的解决方案,因为它使用 f 字符串。enumerate用于不必将行号存储在另一个引用中。
from openpyxl import Workbook
data = {
'australia-central': 0.0097,
'usgov-virginia': 0.00879,
}
workbook = Workbook()
sheet = workbook.active
sheet["A1"] = "Key"
sheet["B1"] = "Hourly"
for row, (key, hourly) in enumerate(data.items(), start=2):
sheet [f"A{row}"] = key
sheet [f"B{row}"] = hourly
workbook.save("output.xlsx")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12301 次 |
| 最近记录: |