cant import exported file from appadmin

84 views
Skip to first unread message

黄祥

unread,
Jun 11, 2021, 5:44:52 AM6/11/21
to web2py-users
step to reproduce
1 create new app
2 edit db.py at the bottom :
db.define_table('mytable', Field('myfield'))

3 insert data on appadmin
4 export as csv file on appadmin
5 import the exported file
no data added and have flash error
<div>unable to parse csv file<pre>iterator should return strings, not bytes (did you open the file in text mode?)</pre></div> 

any idea or fix about this ?

thanks and best regards,
stifan

Clemens

unread,
Jun 11, 2021, 12:43:17 PM6/11/21
to web2py-users
Could be a Python3 issue. I don't export my DBs via appadmin but via an admin controller with the following code:

PY2 = sys.version_info[0] == 2
...
filename = <your filename>
if PY2:
     db.export_to_csv_file(open(filename, 'wb'))
else:
    db.export_to_csv_file(open(filename, 'w', encoding='utf-8', newline=''))

Importing as follows:
filename = <your filename>
if PY2:
    db.import_from_csv_file(open(filename, 'rb'))
    db.commit()
else:
    db.import_from_csv_file(open(str(filename), 'r', encoding='utf-8'))
    db.commit()

And before re-importing the database, first drop all tables.

Have a try, hope it helps!

Best regards
Clemens

黄祥

unread,
Jun 11, 2021, 4:41:50 PM6/11/21
to web2py-users
thanks need to convert the byte object into str object
e.g.

# download_csv
def download_csv():
    stream = StringIO()
    db.export_to_csv_file(stream, 
                          delimiter = ',', 
                          quotechar = '"', 
                          write_colnames = True, 
                          represent = False)
    response.headers['Content-Type'] = 'text/csv'
    response.headers['Content-Disposition'] = 'inline; filename="{0}.csv"'.format(request.function.replace('_', ' ').title() )
    return stream.getvalue()

# upload_csv
def upload_csv():
    header = spt_form_constructor.form_header(T('Upload CSV') )
    form = FORM(INPUT(_type = 'file', _name = 'csv_file'),
                INPUT(_type = 'submit', _value = T('Import') ) )
    if form.process().accepted:
        if request.vars.csv_file != None:
            csv_file = request.vars.csv_file.file.read()
            text_obj = csv_file.decode('UTF-8')
            db.import_from_csv_file(StringIO(text_obj) )

            response.flash = T('Form accepted')
    elif form.errors:
        response.flash = T('Form has errors')
    else:
        response.flash = T('Please fill the form')
    return dict(header = header, form = form)

Reply all
Reply to author
Forward
0 new messages