jazzband / prettytable

Display tabular data in a visually appealing ASCII table format
https://pypi.org/project/PrettyTable/
Other
1.35k stars 154 forks source link

x.get_csv_string() method no longer works #233

Closed ewyluda closed 1 year ago

ewyluda commented 1 year ago

x.get_csv_string() no longer works... I've been using this method for 6 months or so without any issue, went to rerun my code today and i keep getting an attribute error... any suggestions?

# version 2 (nonpublic api)
headers = {'Content-type': 'application/json'}
data = json.dumps(input_dictionary)
p = requests.post('https://api.bls.gov/publicAPI/v2/timeseries/data/', data=data, headers=headers)
json_data = json.loads(p.text)
for series in json_data['Results']['series']:
    x=prettytable.PrettyTable(["series id","year","period","value","footnotes"])
    seriesId = series['seriesID']
    for item in series['data']:
        year = item['year']
        period = item['period']
        value = item['value']
        footnotes=""
        for footnote in item['footnotes']:
            if footnote:
                footnotes = footnotes + footnote['text'] + ','
        if 'M01' <= period <= 'M12':
            x.add_row([seriesId,year,period,value,footnotes[0:-1]])
    output = open('/content/drive/MyDrive/_colab_data_stuff/cpi/csv/' + seriesId + '.csv','w')
    output.write (x.get_csv_string())
    output.close()

image

hugovk commented 1 year ago

What versions are you using?

Has any of these versions changed recently?

hugovk commented 1 year ago

Also, please can you create a self-contained, runnable, minimal reproducer?

ewyluda commented 1 year ago

here is a reproducer:

I am using Google Colab to run this code, the reproducible example below is for a single series, my code functions similarly with the exception of multiple series being requested versus two series here.

What's supposed to happen: an API request is made, prettytable is used to display the ASCII data and then export the tables as individual csv files (for each series ID) to a directory.

When did the error occur: I only started receiving the error today, I have been running the code successfully for 6-8 months until today. I am using Google Colab notebook to run the code, so I am not aware of any updates.

import requests
import json
import prettytable
headers = {'Content-type': 'application/json'}
data = json.dumps({"seriesid": ['CUUR0000SA0','SUUR0000SA0'],"startyear":"2012", "endyear":"2023"})
p = requests.post('https://api.bls.gov/publicAPI/v1/timeseries/data/', data=data, headers=headers)
json_data = json.loads(p.text)
for series in json_data['Results']['series']:
    x=prettytable.PrettyTable(["series id","year","period","value","footnotes"])
    seriesId = series['seriesID']
    for item in series['data']:
        year = item['year']
        period = item['period']
        value = item['value']
        footnotes=""
        for footnote in item['footnotes']:
            if footnote:
                footnotes = footnotes + footnote['text'] + ','

        if 'M01' <= period <= 'M12':
            x.add_row([seriesId,year,period,value,footnotes[0:-1]])
    output = open(seriesId + '.txt','w')
    output.write (x.get_csv_string())
    output.close()
hugovk commented 1 year ago

Thanks, it's working for me, I get these files:

What versions are you using? I have:

>>> import sys
>>> sys.version
'3.11.2 (main, Feb 16 2023, 02:55:59) [Clang 14.0.0 (clang-1400.0.29.202)]'
>>> import prettytable
>>> prettytable.__version__
'3.6.0'

(The reproducer could be made more minimal by using some hardcoded values rather than downloading via an API, it took 1m16s to run and at first I thought it had hung.)

ewyluda commented 1 year ago

ok just found out my python version is 3.9.16 (main, Dec 7 2022, 01:11:51) [GCC 9.4.0] and prettytable is 0.7.2

I need to figure out how to install the latest version in Colab, currently I've just been using

pip install prettytable
import prettytable
hugovk commented 1 year ago

Aha! That makes sense, get_csv_string() was added in 1.0.

https://github.com/jazzband/prettytable/blob/main/CHANGELOG.md#prettytable-10---october-4-2020

Good luck with updating Colab.

hugovk commented 1 year ago

PS Try something like pip install --upgrade prettytable

ewyluda commented 1 year ago
!pip install prettytable --upgrade

worked, thank you!