pythonpyexcel

Disable Pyexcel sorting


I'm using pyexcel and when I get a sheet from a dictionary it gets sorted alphabetically. How can I disable that feature?

I tried searching for information about sorting in the documentation but didn't find anything.

For example in this code I have two rows: Vegetables and Fruits, in that order. But in the output it changes the order sorting it alphabetically.

Code:

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)

Output:

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

Expected Output:

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

Solution

  • Made it using OrderedDict() collections.OrderedDict

    food = OrderedDict({
        'Vegetables': vegetables,
        'Fruits': fruits,
    })