Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell scipt或python脚本将时间转换为人类可读的格式_Python_Linux_Bash_Python 2.7_Shell - Fatal编程技术网

Shell scipt或python脚本将时间转换为人类可读的格式

Shell scipt或python脚本将时间转换为人类可读的格式,python,linux,bash,python-2.7,shell,Python,Linux,Bash,Python 2.7,Shell,我有一个日志中类似20170103154828 pwcg12.apache.web 39558809870的时间格式,但我希望以人类可读的格式2017-01-03 15:48:28 pwcg12.apache.web 39558809870查看它。由于它不是由服务器生成的历元时间,因此应用历元转换在这方面不起作用。任何帮助都将不胜感激 谢谢这里有一个非常简单而不复杂的方法来实现这一点 time = "20170103154828" first = time[:4] + '-' + time[4

我有一个日志中类似20170103154828 pwcg12.apache.web 39558809870的时间格式,但我希望以人类可读的格式2017-01-03 15:48:28 pwcg12.apache.web 39558809870查看它。由于它不是由服务器生成的历元时间,因此应用历元转换在这方面不起作用。任何帮助都将不胜感激


谢谢

这里有一个非常简单而不复杂的方法来实现这一点

time = "20170103154828"

first = time[:4] + '-' + time[4:]
second = first[:7] + '-' + first[7:]
third = second[:10] + ' ' + second[10:]

hour = third[:13] + ':' + third[13:]
final = hour[:16] + ':' + hour[16:]
print(final)
收益率:

2017-01-03 15:48:28

只需使用子字符串操作提取字符串的各个部分,然后将它们与-和:.datetime.strftime和datetime.strftime.strftw连接起来,也许您没有注意到这只是一个没有标点符号的可读时间。欢迎使用堆栈溢出!StackOverflow期待您的加入,我们也会。请更新您的问题,以显示您已在某个应用程序中尝试过的内容。有关更多信息,请参阅,并使用:我导入了时间并使用了strftime,但它在转换它时不起作用。