python - 在YYYY-MM-DD HH :MM[:SS[. SSSSSS中转换日期时间]]

标签 python python-2.7 google-bigquery

我收到此错误:

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

尝试插入行时从BigQuery中获取。

这是我的代码:
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)

如何修复日期格式?

最佳答案

如果这确实是您所需要的。

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

方括号更有可能表示可选部件。所以:
now = datetime.now().strftime("%Y-%m-%d %H:%M")

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

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

关于python - 在YYYY-MM-DD HH :MM[:SS[. SSSSSS中转换日期时间]],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43553222/

相关文章:

python - 计算并绘制 [-1, 5] 区间内的理论正态分布 N(2, 1)

python - 如何在不使用 import * 的情况下导入导入其他模块的模块

python - 谷歌CP : Where to schedule PubSub subscriber which writes to BigQuery

python - 访问 .tar 文件中的 .tar 文件

python - "freeze" tensorflow 中的一些变量/范围 : stop_gradient vs passing variables to minimize

python - 尝试在 for 循环之外使用变量会出现 SyntaxError : no binding for nonlocal 'max_' found

python - 函数返回无

google-app-engine - App Engine Python 模块和 channel 服务

sql - BigQuery - 连接谓词中不支持带表的子查询

json - 通过 jq 使一个(子)JSON 对象出现在一行上