[英]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. 当我试图插入一行时从BigQuery。
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? 如何修复日期格式?
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")
format your date into this YYYY-MM-DD HH:MM:SS meaning the month day hour minutes and second should be in two digits format. 将您的日期格式化为此YYYY-MM-DD HH:MM:SS表示月份日小时分钟,秒钟应为两位数格式。
sample: 样品:
2018-02-16 04:39:05 Correct 2018-02-16 04:39:05正确
2018-2-16 4:39:5 wrong 2018-2-16 4:39:5错了
Happy coding! 快乐的编码!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.