Last active
April 6, 2020 11:40
-
-
Save lucascheung/bd1132526103ce4179afa8a083ad996f to your computer and use it in GitHub Desktop.
Python dictionary to various output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bigquery_schema_generator.generate_schema import SchemaGenerator | |
import json | |
from datetime import datetime | |
import gspread | |
from oauth2client.service_account import ServiceAccountCredentials | |
from df2gspread import df2gspread as d2g | |
def output_helper(data, output): | |
level = level.lower() | |
date = datetime.today().strftime('%Y%m%d') | |
if output == 'dict': | |
return data | |
elif output == 'json': | |
data_json = json.dumps(data) | |
return data_json | |
elif output == 'json-file': | |
with open(f'{{date}.json', 'w') as outfile: | |
json.dump(data, outfile) | |
elif output == 'json-bq': | |
with open(f'{date}.json', 'w') as outfile: | |
for item in data: | |
print(json.dumps(item), file=outfile) | |
#Create BQ schema | |
generator = SchemaGenerator(keep_nulls=True) | |
input_file = open(f'{{date}.json', 'rb') | |
output_file = open(f'bq_schema_{date}.json', 'w') | |
generator.run(input_file, output_file) | |
else: | |
data_json = json.dumps(data) | |
return data_json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment