如何使用Python将.csv格式文件转换为.tde格式文件(tableau extract)?

时间:2016-09-12 11:51:32

标签: python csv extract tableau

以下是我面临的问题

我无法使用以下命令正确安装dataextract包,因此在导入包时会遇到问题。

"python -m pip install dada extract"

还面临tableau_rest_api

中以下命令中的问题
 TableauRestApi(server, username, password)

任何人都可以告诉我执行相同任务的其他方法或帮助我纠正现有代码

    #######convert .csv file to .tde file#########################
import os,csv,datetime
import dataextract as tde
# INSERT EXCEPT CONDITION
# EXTRACT FILE AND OPEN .CSV
try:
    tdefile = tde.Extract('trial1.tde')
except:
    os.remove('trial1.tde')``
    tdefile = tde.Extract('trial1.tde')

csvReader= csv.reader(open('test1.csv','rb'),delimiter=',',quotechar='"')

# CREATE THE TABLE DEF
tableDef= tde.TableDefinition()
tableDef.addColumn('Country',tde.Type.CHAR_STRING)
tableDef.addColumn('sales',tde.Type.INTEGER)
tableDef.addColumn('units',tde.Type.INTEGER)

# CREATE TABLE
table=tdefile.addTable('C:\Users\Mahita.Gm\Desktop\Extract',tableDef)

# DATA TO TDE

newrow= tde.Row(tableDef)
csvReader.next()
for line in csvReader:
    newrow.setCharString(0,str(line[0]))
    newrow.setInteger(1,int(line[1]))
    newrow.setInteger(2,int(line[2]))
    table.insert(newrow)

# closing tde
tdefile.close()

#############code to publish tableau extract in to server #################
import tableau_rest_api
from tableau_rest_api.tableau_rest_api import * import urllib2
username = '<user name>'
password = '<password>'
server = '<server address>'
logger = Logger('publish.log')

test = TableauRestApi(server, username, password)
test.enable_logging(logger)
test.signin()

test_ds_proj_luid = test.query_project_luid_by_name('default')
#test_ds_wb_luid = test.query_workbook_luid_by_name('test_1_c_CF')

#test_ds_p_luid = test.query_datasource_luid_by_name_in_project('trial1', 'test_1_c_CF')
# Publishing up my first data source to Test, from disk
new_ds_luid = test.publish_datasource('trial1.tde', 'trial1', test_ds_proj_luid, overwrite=True)

0 个答案:

没有答案