Importing CSV file

77 views
Skip to first unread message

Joe

unread,
Feb 26, 2016, 4:46:52 AM2/26/16
to web2py-users
I tried to import a CSV file  but all the values are None.

This is what I did:

def index():
    if request.vars.csvfile != None:
        table = db[request.vars.table]
        file = request.vars.csvfile.file
        table.import_from_csv_file(file)
    return dict()

My tables look like this:

db.define_table('animation',
    Field('activity', widget=SQLFORM.widgets.radio.widget, requires=IS_IN_SET({'Co-prod', 'Buyer','Sales', 'Producer', 'Director'})), Field('business', widget=SQLFORM.widgets.radio.widget, requires=IS_IN_SET({'Distribution', 'Broadcaster','Production', 'Internet', 'Financing', 'MNO', 'Press'})), Field('company', 'string', requires=[IS_NOT_EMPTY()]), Field('country', requires=[IS_NOT_EMPTY()]), Field('email', requires=[IS_NOT_EMPTY(), IS_EMAIL()]), Field('first_name', 'string',requires=IS_NOT_EMPTY()), Field('last_name', 'string',requires=IS_NOT_EMPTY()), Field('position_title', 'string',requires=IS_NOT_EMPTY()), Field('website', 'string',requires=[IS_NOT_EMPTY(), IS_URL]), Field('date_made', 'datetime', default = request.now, writable=False, readable=False, requires = IS_DATE(format=('%d-%m-%Y'))))

My view:
{{extend 'layout.html'}}
{{=FORM(INPUT(_type='file',_name='csvfile'),INPUT(_type='hidden',_value='animation',_name='table'),INPUT(_type='submit',_value='Upload'))}}

The CSV file has the same headers as the table names. The CSV is imported, the length of the rows in the database are the same as in the CSV file but the records are all None.
What I want to do is to be able to import CSV files to this db as well as input values manually using a form. This is why I am using a radio.widget.
What am I doing wrong?

Thanks.

Joe

Anthony

unread,
Feb 26, 2016, 7:56:07 AM2/26/16
to web2py-users
Maybe manually enter a few records, export to CSV, and inspect the exported file to make sure your imported file has everything in the proper format.

Anthony

villas

unread,
Feb 26, 2016, 8:30:40 AM2/26/16
to web2py-users
I'm not sure whether this will be of any help,  but I have found the following useful when working with CSV files in the past...

Joe

unread,
Feb 26, 2016, 9:21:42 AM2/26/16
to web2py-users
Hello Anthony, Thanks for your reply.
I exported the CSV after adding some records, as you suggested, and I see no difference in the format. I tried to change the format several times but I can't import it. I either get the None records or an error depending on how I tried to modify the format.

Johann Spies

unread,
Mar 1, 2016, 8:45:13 AM3/1/16
to web...@googlegroups.com
On 26 February 2016 at 16:21, Joe <degrou...@gmail.com> wrote:
Hello Anthony, Thanks for your reply.
I exported the CSV after adding some records, as you suggested, and I see no difference in the format. I tried to change the format several times but I can't import it. I either get the None records or an error depending on how I tried to modify the format.

The following process works normally for me:

export an empty table using the admin-interface.

Open the exported file as well as the csv-file in a spreadsheet  (localc in my case).

Copy the second csv-file into the the exported started with column 1 row 2.

Make sure the data columns and headers correspond to that of the exported one.
Delete the row containing the second file's headers.
Save it as CSV-file and then import it.

Regards
Johann
--
Because experiencing your loyal love is better than life itself,
my lips will praise you.  (Psalm 63:3)
Reply all
Reply to author
Forward
0 new messages