0

我正在使用 pyexcel,当我从字典中获取一张表时,它会按字母顺序排序。如何禁用该功能?

我尝试在文档中搜索有关排序的信息,但没有找到任何东西。

例如,在这段代码中,我有两行:蔬菜和水果,按顺序排列。但在输出中,它会更改按字母顺序排序的顺序。

代码:

import pyexcel as p

vegetables = ['tomato', 'corn', 'onion']
fruits = ['banana', 'apple', 'orange']

food = {
    'Vegetables': vegetables,
    'Fruits': fruits,
}

sheet = p.get_sheet(adict=food)
print(sheet)

输出:

pyexcel_sheet1:
+--------+------------+
| Fruits | Vegetables |
+--------+------------+
| banana | tomato     |
+--------+------------+
| apple  | corn       |
+--------+------------+
| orange | onion      |
+--------+------------+

预期输出:

pyexcel_sheet1:
+------------+---------+
| Vegetables | Fruits  |
+------------+---------+
| tomato     | banana  |
+------------+---------+
| corn       | apple   |
+------------+---------+
| onion      | orange  |
+------------+---------+
4

1 回答 1

0

使用 OrderedDict() collections.OrderedDict

food = OrderedDict({
    'Vegetables': vegetables,
    'Fruits': fruits,
})
于 2019-08-23T00:25:52.463 回答