I am just testing the Data Extract API and cannot get it to work even after watching the video and nearly copying the code word for word. What am I doing wrong?
My code:
import dataextract as tde
import csv,os,datetime
#Step 1: Create the extract file and open the .csv
try:
tdefile = tde.Extract('APITest.tde')
except:
os.remove('APITest.tde')
tdefile = tde.Extract('APITest.tde')
csvReader = csv.reader(open('U:/Neil/Tableau/APITest.csv','rb'), delimiter=',',quotechar='"')
#Step 2: Create the tableDef
tableDef = tde.TableDefinition()
tableDef.addColumn('ID', tde.Type.CHAR_STRING) #1
tableDef.addColumn('LOB', tde.Type.CHAR_STRING) #2
tableDef.addColumn('GL Acct Num', tde.Type.CHAR_STRING) #3
tableDef.addColumn('CY YTD', tde.Type.DOUBLE) #4
tableDef.addColumn('CY Audit ME', tde.Type.DOUBLE) #5
tableDef.addColumn('CY Jan', tde.Type.DOUBLE) #6
tableDef.addColumn('PY Jan', tde.Type.DOUBLE) #7
tableDef.addColumn('PY Audit ME', tde.Type.DOUBLE) #8
tableDef.addColumn('PY Dec', tde.Type.DOUBLE) #9
tableDef.addColumn('PPY Jan', tde.Type.DOUBLE) #10
tableDef.addColumn('PPY Audit ME', tde.Type.DOUBLE) #11
tableDef.addColumn('PPY Dec', tde.Type.DOUBLE) #12
#Step 3: Create the table in the image of the tableDef
table = tdefile.addTable('Extract',tableDef)
#Step 4: Loop through csv, grab data, put into rows, insert rows into table.
newrow = tde.Row(tableDef)
csvReader.next()
for line in csvReader:
newrow.setCharString(0, str(line[0]))
newrow.setCharString(1, str(line[1]))
newrow.setCharString(2, str(line[2]))
newrow.setDouble(3, float(line[3]))
newrow.setDouble(4, float(line[4]))
newrow.setDouble(5, float(line[5]))
newrow.setDouble(6, float(line[6]))
newrow.setDouble(7, float(line[7]))
newrow.setDouble(8, float(line[8]))
newrow.setDouble(9, float(line[9]))
newrow.setDouble(10, float(line[10]))
newrow.setDouble(11, float(line[11]))
newrow.setDouble(12, float(line[12]))
table.insert(newrow)
#Step 5: Close the tde
tdefile.close()
My error:
Traceback (most recent call last):
File "C:/Python27/APIDataExtractTest.py", line 29, in <module>
table = tdefile.addTable('Extract',tableDef)
File "C:\Python27\lib\site-packages\dataextract\Base.py", line 484, in addTable
raise Exceptions.TableauException(result, wstring_at(tablib.TabGetLastErrorMessage()))
TableauException: TableauException (303): duplicate table name
Your issue must be related to the TDE file (APITest.tde) not getting deleted when you run the script more than once. First thing to try is to manually delete the APITest.tde file & see if the error still happens. If it works after manually deleting the file, it means that the script is not able to delete the file on its own. If that is the case, you would have to troubleshoot why your script can't delete the file. Some possible reasons:
* The try statement is not causing an exception, so the script does not actually delete the file.
* You have the TDE file open in Tableau Desktop
* You do not have user permissions to delete the file
When I tried running this script on my machine, I ran into the same issue. It appears that the try statement does not cause an exception, so the line with "os.remove... " is _never_ called. I fixed the problem by always calling os.remove("APITest.tde") before attempting to create a new extract.
Let us know how this plays out,
-Steve