简体   繁体   English

如何将 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

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.

相关问题 使用 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 Python:如何将 UTC 时间戳从 mm/dd/yyyy hh:mm:ss AM 更改为 dd/mm/yyyy hh:mm:ss AM - Python: How to change a UTC timestamp from mm/dd/yyyy hh:mm:ss AM to dd/mm/yyyy hh:mm:ss AM 在YYYY-MM-DD中转换日期时间HH:MM [:SS [.SSSSSS]] - Transform datetime in YYYY-MM-DD HH:MM[:SS[.SSSSSS]] 如何拆分时间数据(格式为yyyy-mm-dd hh:mm:ss)以测试和训练集合? - How to split time data (in format of yyyy-mm-dd hh:mm:ss) to test and train sets? 将字符串 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 日期时间转换格式为 dd/mm/yyyy HH:MM:SS - Python Datetime convert format into dd/mm/yyyy HH:MM:SS 将日期格式从 csv. 文件中的 python 从 YYYY-MM-DD HH:MM:SS+00:00 转换为 YYYY-MM-DD - Converting a date format from a csv. file in python from YYYY-MM-DD HH:MM:SS+00:00 to YYYY-MM-DD 转换(YYYY-MM-DD-HH:MM:SS)日期时间 - Converting (YYYY-MM-DD-HH:MM:SS) date time 将带时区的日期格式更改为 yyyy-mm-dd hh:mm:ss - change date format with timezone to yyyy-mm-dd hh:mm:ss 在 python 中将日期从 DD/MM/YYYY HH.mm.ss 转换为 MM-DD-YYYY HH:MM:SS 时出错 - Error converting date from DD/MM/YYYY HH.mm.ss to MM-DD-YYYY HH:MM:SS in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM