Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use import_set #166

Closed
mazing opened this issue Aug 31, 2014 · 5 comments
Closed

Use import_set #166

mazing opened this issue Aug 31, 2014 · 5 comments
Labels

Comments

@mazing
Copy link

mazing commented Aug 31, 2014

It seems it's not documented how to import a file and loop through the rows and columns.

Can someone help me out with a small example?

In the API the function import_set() is listed, but I don't know how to use this function.

@rabinnankhwa
Copy link
Contributor

Read a file and set the contents as input to the function import_set.
For eg:

import tablib
data = open("test.csv").read()
dataset = tablib.import_set(data)
header = dataset.headers
rows = dataset._data
for row in rows:
    print row

@ghost
Copy link

ghost commented Feb 10, 2015

i've tried the open().read() but only return None object from the tablib.import_set(). I'm pretty sure the filepath is correct because i can read the whole content of the file with "".join(open("path")).

            csvfiles = glob.glob( 'backup_db/{date}/*.csv'.format( date=args[0] ) ) # args[0] = '2015_05_02' (like year, month, day)

            for csvf in csvfiles : # csvf = 'backup_db/2015_05_02/SomeModel.csv'

                #print( 'importing ' , csvf , '\n' , ''.join(open(csvf,'r')) )

                dataset = tablib.import_set( open(csvf,'r').read() )

                print( 'dataset -> ' , dataset ) # None

                modelname = os.path.basename(csvf)[:-4]

                print( modelname+'Resource' )

                model = getattr(admin,modelname+'Resource')()

                print(dataset)

                model.import_data(dataset=dataset,dry_run=True) # Error - > 'NoneType' object has no attribute 'dict'

@ghost
Copy link

ghost commented Jun 5, 2015

Same issue.

>>> import tablib
>>> print tablib.import_set(open("derp.xls", "rb"))
None

File is existing

@casimir
Copy link

casimir commented Mar 7, 2016

Just encountered this issue with a CSV file. When using ; as separator tablib.import_set return None, whereas when using , it return the correct dataset.

@claudep
Copy link
Contributor

claudep commented Jan 27, 2020

Please reopen if you still have issues on the latest release.

@claudep claudep closed this as completed Jan 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants