[英]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
I'm trying to upload a CSV file to BigQuery, the required data type for the schema field for the date is "DATETIME"我正在尝试将 CSV 文件上传到 BigQuery,日期的架构字段所需的数据类型是“DATETIME”
" "
bigquery.SchemaField("DATE", "DATETIME", mode="REQUIRED") bigquery.SchemaField("DATE", "DATETIME", mode="REQUIRED")
" "
but in my CSV file, the format is DD:MM:YY HH:MM, and I'm receiving the error of 'Invalid DateTime string "21.03.22 22:02"'.但在我的 CSV 文件中,格式为 DD:MM:YY HH:MM,并且我收到“Invalid DateTime string "21.03.22 22:02"”的错误。 I would appreciate if anyone can help me convert the date format into CSV via python.
如果有人可以帮助我通过 python 将日期格式转换为 CSV,我将不胜感激。
from dateutil import parser
date = parser.parse("21.03.22 22:02")
Output输出
2022-03-21 22:02:00
use a parser.使用解析器。 If you can use the data in pandas, then you can convert the data in one go using pd.to_datetime.
如果您可以使用 pandas 中的数据,那么您可以使用 pd.to_datetime 一次性转换数据。
import pandas as pd
df = pd.DataFrame({'A':[1, 2, 3],
'Data':["21.03.22 22:02", "22.03.22 22:02", "23.03.22 22:02"]})
print(df)
df['Data'] = pd.to_datetime(df['Data'])
print(df)
before前
A Data
0 1 21.03.22 22:02
1 2 22.03.22 22:02
2 3 23.03.22 22:02
after后
A Data
0 1 2022-03-21 22:02:00
1 2 2022-03-22 22:02:00
2 3 2022-03-23 22:02:00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.