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
本文介绍了Python:如何将UTC时间戳从mm/dd/yyyy hh:mm:ss AM更改为dd/mm/yyyy hh:mm:ss AM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试更改csv文件中UTC时间戳的格式,但是似乎没有任何效果.它总是说有一个ValueError 与格式'%m/%d/%Y%I:%M:%S%p'不匹配
下面是CSV列的示例
I am trying to change the format of a UTC timestamp in a csv file but nothing seems to be working.
It keeps saying that there is a ValueError does not match format '%m/%d/%Y %I:%M:%S %p'
Below there is an example of the CSV column
df[' UTC Event Timestamp'] = df[' UTC Event Timestamp'].astype(str)
string_col = str(df)
string_col.strip()
datetime.strptime(string_col, '%m/%d/%Y %I:%M:%S %p')
datetime.strptime(string_col, '%m/%d/%Y %I:%M:%S %p').strftime('%d/%m/%Y %I:%M:%S %p')
print(string_col)
推荐答案
您可以使用
df['string_col'].apply(lambda row: datetime.strptime(row, '%m/%d/%Y %I:%M:%S %p').strftime('%d/%m/%Y %I:%M:%S %p'))
将列转换为日期时间格式.
to convert the column in to datetime format.
这篇关于Python:如何将UTC时间戳从mm/dd/yyyy hh:mm:ss AM更改为dd/mm/yyyy hh:mm:ss AM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文