Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform datetime in YYYY-MM-DD HH:MM[:SS[.SSSSSS]]

I'm receiving this error:

Could not parse 'event_date' as a timestamp. Required format is YYYY-MM-DD HH:MM[:SS[.SSSSSS]]

from BigQuery when I'm trying to insert a row.

This is my code:

bigquery_client = bigquery.Client.from_service_account_json(CREDENTIALS_BIGQUERY, 'roas-164016')
dataset = bigquery_client.dataset(BQ_LOGS_DATASET_NAME)
table = dataset.table(BQ_EMAIL_SENDS_TABLE_NAME)

data = {}
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
data['send_id'] = 'test'
data['uid'] = 'test'
data['account_id'] = 'test'
data['subaccount_id'] = 'test'
data['event_id'] = 'test'
data['event_date'] = now
data['html_content'] = 'test'
data['campaign_name'] = 'test'
data['subject'] = 'test'
data['send_type'] = 'test'

json_data = json.dumps(data)

data =  json.loads(json_data)
table.reload()

rows = [data]
errors = table.insert_data(rows)

How can I fix the date formatting?

like image 421
Filipe Ferminiano Avatar asked Apr 21 '17 23:04

Filipe Ferminiano


1 Answers

If that's literally what you need.

now = datetime.now().strftime("%Y-%m-%d %H:%M[:%S[.%f]]")

More likely, the square brackets indicate optional parts. So:

now = datetime.now().strftime("%Y-%m-%d %H:%M")

or

now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

or

now = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
like image 131
Batman Avatar answered Nov 06 '22 05:11

Batman