How to export a list of list into a file?

Given the following Panda DataFrame:

import pandas as pd
data = {'product': ['Matcha Latte', 'Milk Tea', 'Cheese Cocoa', 'Walnut Brownie'],
         '2015': [43.3, 83.1, 86.4, 72.4],
         '2016': [85.8, 73.4, 65.2, 53.9],
         '2017': [93.7, 55.1, 82.5, 39.1]}
df = pd.DataFrame(data)

df:

|----------------+------+------+------|
| product        | 2015 | 2016 | 2017 |
|----------------+------+------+------|
| Matcha Latte   | 43.3 | 85.8 | 93.7 |
| Milk Tea       | 83.1 | 73.4 | 55.1 |
| Cheese Cocoa   | 86.4 | 65.2 | 82.5 |
| Walnut Brownie | 72.4 | 53.9 | 39.1 |
|----------------+------+------+------|

That I’m converting to Echarts dataset format:

content = df.T.reset_index().T.values.tolist()

content:

[['product', '2015', '2016', '2017'],
 ['Matcha Latte', 43.3, 85.8, 93.7],
 ['Milk Tea', 83.1, 73.4, 55.1],
 ['Cheese Cocoa', 86.4, 65.2, 82.5],
 ['Walnut Brownie', 72.4, 53.9, 39.1]]

I’m trying to export the result (content) to a file, and ran the following code:

import json
with open('output', 'w') as foo:
    json.dump(content, foo)

But it gave me this:

[['product', '2015', '2016', '2017'], ['Matcha Latte', 43.3, 85.8, 93.7], ['Milk Tea', 83.1, 73.4, 55.1], ['Cheese Cocoa', 86.4, 65.2, 82.5], ['Walnut Brownie', 72.4, 53.9, 39.1]]

How could I export content to a file looking like this:

[['product', '2015', '2016', '2017'],
 ['Matcha Latte', 43.3, 85.8, 93.7],
 ['Milk Tea', 83.1, 73.4, 55.1],
 ['Cheese Cocoa', 86.4, 65.2, 82.5],
 ['Walnut Brownie', 72.4, 53.9, 39.1]]

  • I don’t believe that’s the output you’re actually getting. JSON uses double quotes, not single quotes.

    – 

  • When parsing a JSON file, it doesn’t matter if it’s on separate lines or the same line. Why do you care how it’s formatted?

    – 

  • json.dump() has an indent option that will make it spread over multiple lines. But it will put each nested list element on its own line as well, it won’t put the inner lists on the same lines.

    – 

  • @Barmar. You should reopen the question while waiting for the OP to answer your questions?

    – 

  • I just copy pasted the json.dumps output. Formatting makes it easier to read. indent didn’t provide the expected result. The actual table has +100 columns.

    – 

Unless I’m mistaken, you’re just looking to print your list nicely. You can do that with pprint module:

from pprint import pprint

with open('output.txt', mode="w") as foo:
    pprint(content, stream=foo)

Output:

>>> cat 'output.txt'
[['product', '2015', '2016', '2017'],
 ['Matcha Latte', 43.3, 85.8, 93.7],
 ['Milk Tea', 83.1, 73.4, 55.1],
 ['Cheese Cocoa', 86.4, 65.2, 82.5],
 ['Walnut Brownie', 72.4, 53.9, 39.1]]

Note: this method is probably not robust for long list of records.

Edit: another way to do the same with print statement:

with open('output.txt', mode="w") as foo:
    for i, row in enumerate(content):
        if i == 0:
            print(f'[{row},', file=foo)
        elif i == len(content)-1:
            print(f' {row}]', file=foo)
        else:
            print(f' {row},', file=foo)

Why not try a json file format ?

import json
import pandas as pd


data = {'product': ['Matcha Latte', 'Milk Tea', 'Cheese Cocoa', 'Walnut Brownie'],
         '2015': [43.3, 83.1, 86.4, 72.4],
         '2016': [85.8, 73.4, 65.2, 53.9],
         '2017': [93.7, 55.1, 82.5, 39.1]}
df = pd.DataFrame(data)

with open('output.json', 'w') as foo:
    json.dump(df.to_dict(orient="records"), foo, indent=4)

File content :

[
    {
        "product": "Matcha Latte",
        "2015": 43.3,
        "2016": 85.8,
        "2017": 93.7
    },
    {
        "product": "Milk Tea",
        "2015": 83.1,
        "2016": 73.4,
        "2017": 55.1
    },
    {
        "product": "Cheese Cocoa",
        "2015": 86.4,
        "2016": 65.2,
        "2017": 82.5
    },
    {
        "product": "Walnut Brownie",
        "2015": 72.4,
        "2016": 53.9,
        "2017": 39.1
    }
]

Code for list of list

import json
import pandas as pd

data = {'product': ['Matcha Latte', 'Milk Tea', 'Cheese Cocoa', 'Walnut Brownie'],
         '2015': [43.3, 83.1, 86.4, 72.4],
         '2016': [85.8, 73.4, 65.2, 53.9],
         '2017': [93.7, 55.1, 82.5, 39.1]}
df = pd.DataFrame(data)

# Convert DataFrame to a list of lists
content = df.values.tolist()

with open('output.json', 'w') as foo:
    json.dump(content, foo, indent=4)
[
    [
        "Matcha Latte",
        43.3,
        85.8,
        93.7
    ],
    [
        "Milk Tea",
        83.1,
        73.4,
        55.1
    ],
    [
        "Cheese Cocoa",
        86.4,
        65.2,
        82.5
    ],
    [
        "Walnut Brownie",
        72.4,
        53.9,
        39.1
    ]
]

Leave a Comment