[Python] How to display pretty tables in terminal with tabulate python package?

Discussão em 'Python' iniciado por Stack, Abril 26, 2025.

  1. Stack

    Stack Membro Participativo

    I am have simple CLI app written in python with argparse module. Basically I am fetching some cryptocurrency data with external api, I transform it into pandas dataframe and print it with tabulate module. But I have issue with printing tables in my terminal. When I am doing that everything goes wrong and the tables do not maintain the proper structure and in the end all looks like on screen belowe.

    [​IMG]

    Here is snippet for printing pandas data frame with tabulate

    # file: example.py
    import requests
    from tabulate import tabulate


    def get_recommendations():
    url = "https://min-api.cryptocompare.com/data/recommended/all"
    req = requests.get(url, params={'fsym' : 'BTC' ,"tsym": 'USD'})
    return pd.DataFrame(req.json()['Data']['exchanges']).T


    df = get_recommendations()

    print(
    tabulate(
    df,
    headers=df.columns,
    floatfmt=".5f",
    showindex=True,
    tablefmt="psql",
    )
    )



    if you run in terminal python example.py you will see mess in terminal like on screenshot above code snippet.

    Can I some how fix it, and display nice, well formatted tables in my terminal like below ? [​IMG]

    Continue reading...

Compartilhe esta Página