如何更新 python 中的时间格式?
How to update time format in python?
在 CSV 文件中我有值
id description
a1 "[00:01.461]my name is john[00:22.95]I live in NY[00:27.137]Works in[00:28.13]media"
b1 "[00:29.13]Hi[00:32.40]How are you"
c1 "[00:36.401]see you soon[00:41.331]Bye"
我想要句号(.)后的两位数字,所以[00:01.461]将是[00:01.46]所以时间格式应该是[xx:xx.xx]
期望的输出:
id description
a1 "[00:01.46]my name is john[00:22.95]I live in NY[00:27.13]Works in[00:28.13]media"
b1 "[00:29.13]Hi[00:32.40]How are you"
c1 "[00:36.40]see you soon[00:41.33]Bye"
如何在 python 或数据库端执行此操作?
您可以使用pandas.str.replace
import pandas as pd
d =[['a1', "[00:01.461]my name is john[00:22.95]I live in NY[00:27.137]Works in[00:28.13]media"],
['b1', "[00:29.13]Hi[00:32.40]How are you"],
['c1', "[00:36.401]see you soon[00:41.331]Bye"]]
df = pd.DataFrame(d, columns=['id', 'description'])
df['description'] = df['description'].str.replace(r'(\[\d{2}:\d{2}.(\d{3})\])', lambda x: x.groups()[0][:7] + x.groups()[1][:2] + x.groups()[0][-1])
df
id description
0 a1 [00:01.46]my name is john[00:22.95]I live in NY[00:27.13]Works in[00:28.13]media
1 b1 [00:29.13]Hi[00:32.40]How are you
2 c1 [00:36.40]see you soon[00:41.33]Bye\n'
在 CSV 文件中我有值
id description
a1 "[00:01.461]my name is john[00:22.95]I live in NY[00:27.137]Works in[00:28.13]media"
b1 "[00:29.13]Hi[00:32.40]How are you"
c1 "[00:36.401]see you soon[00:41.331]Bye"
我想要句号(.)后的两位数字,所以[00:01.461]将是[00:01.46]所以时间格式应该是[xx:xx.xx]
期望的输出:
id description
a1 "[00:01.46]my name is john[00:22.95]I live in NY[00:27.13]Works in[00:28.13]media"
b1 "[00:29.13]Hi[00:32.40]How are you"
c1 "[00:36.40]see you soon[00:41.33]Bye"
如何在 python 或数据库端执行此操作?
您可以使用pandas.str.replace
import pandas as pd
d =[['a1', "[00:01.461]my name is john[00:22.95]I live in NY[00:27.137]Works in[00:28.13]media"],
['b1', "[00:29.13]Hi[00:32.40]How are you"],
['c1', "[00:36.401]see you soon[00:41.331]Bye"]]
df = pd.DataFrame(d, columns=['id', 'description'])
df['description'] = df['description'].str.replace(r'(\[\d{2}:\d{2}.(\d{3})\])', lambda x: x.groups()[0][:7] + x.groups()[1][:2] + x.groups()[0][-1])
df
id description
0 a1 [00:01.46]my name is john[00:22.95]I live in NY[00:27.13]Works in[00:28.13]media
1 b1 [00:29.13]Hi[00:32.40]How are you
2 c1 [00:36.40]see you soon[00:41.33]Bye\n'