简体   繁体   English

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

[英]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.

相关问题 如何使用openpyxl在Excel中使用'yyyy-mm-dd hh:mm:ss'形式的日期时间对象格式化单元格 - How to format cell with datetime object of the form 'yyyy-mm-dd hh:mm:ss' in Excel using openpyxl 将时间戳记('YYYY-MM-DD HH:MM:SS')转换为简单的日期时间格式 - Converting Timestamp('YYYY-MM-DD HH:MM:SS') to simple datetime format Python YYYY-MM-DD hh:mm:ss.fff-zz:xx 格式化日期时间问题 - Python YYYY-MM-DD hh:mm:ss.fff-zz:xx formatted datetime issue 将日期时间格式转换为 Python 中的“YYYY-MM-DD HH:MM:SS”字符串 - Convert datetime format to "YYYY-MM-DD HH:MM:SS" string in Python 将字符串 MM-dd-yyyy hh:mm:ss 转换为 PySpark 中的时间戳 yyyy-MM-dd hh:mm:ss - Convert string MM-dd-yyyy hh:mm:ss to timestamp yyyy-MM-dd hh:mm:ss in PySpark 使用 python 将日期格式 mm/dd/yyyy hh:mm:ss AM 更改为 yyyy-mm-dd hh:mm:ss 格式 - change date format mm/dd/yyyy hh:mm:ss AM to yyyy-mm-dd hh:mm:ss format using python 如何将 csv 文件中的时间格式从 DD:MM:YY HH:MM 更改为 YYYY-MM-DD HH:MM:SS。 或 YYYY/MM/DD HH:MM:SS - How to change time format in a csv file from DD:MM:YY HH:MM to YYYY-MM-DD HH:MM:SS. or YYYY/MM/DD HH:MM:SS Python 日期时间转换格式为 dd/mm/yyyy HH:MM:SS - Python Datetime convert format into dd/mm/yyyy HH:MM:SS 如何在 dataframe(python/pandas)中将日期时间(YYYY-MM-DD HH-MM-SS)缩短为年月(YYYY-MM) - How do I shorten a datetime (YYYY-MM-DD HH-MM-SS) to year-month (YYYY-MM) in a dataframe (python/pandas) 将YYYYMMDD转换为YYYY-MM-DD和HHMMSS转换为HH:MM:SS用于烛台绘图 - Convert YYYYMMDD into YYYY-MM-DD and HHMMSS into HH:MM:SS for candlestick plotting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM