diff --git a/.gitignore b/.gitignore index 994e968..5708398 100644 --- a/.gitignore +++ b/.gitignore @@ -75,3 +75,6 @@ tasks.json #Jekyll docs/_site + +#Local venv +venv/ diff --git a/samples/show-fields/fields_to_csv.py b/samples/show-fields/fields_to_csv.py new file mode 100644 index 0000000..c19f689 --- /dev/null +++ b/samples/show-fields/fields_to_csv.py @@ -0,0 +1,46 @@ +############################################################ +# Step 1) Use Datasource object from the Document API +############################################################ +from tableaudocumentapi import Datasource +import xml.etree.ElementTree as ET +import pandas as pd +from tabulate import tabulate + +############################################################ +# Step 2) Open the .tds we want to inspect +############################################################ +sourceTDS = Datasource.from_file('world.tds') + +############################################################ +# Step 3) Print out all of the fields and what type they are +############################################################ +print('----------------------------------------------------------') +print('--- {} total fields in this datasource'.format(len(sourceTDS.fields))) +print('----------------------------------------------------------') + +field_list = [] + +for count, field in enumerate(sourceTDS.fields.values()): + + if field.description is not None: + description_xml = ET.fromstring(field.description) + description_without_tags = description_xml[0][0].text + else: + description_without_tags = None + + field_dict = {'name': field.name, + 'caption': field.caption, + 'role': field.role, + 'type': field.type, + 'datatype': field.datatype, + 'calculation': field.calculation, + 'default_aggregation': field.default_aggregation, + # 'description': field.description, + 'description_raw': description_without_tags} + + + field_list.append(field_dict) + +df = pd.DataFrame(field_list) +print(tabulate(df, headers='keys', tablefmt='psql')) +