Operating environment
Xeon E5-2620 v4 (8 cores) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 and its-devel
mpich.x86_64 3.1-5.el6 and its-devel
gcc version 4.4.7 (And gfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.Use 1.
Python 3.6.0 on virtualenv
I want to convert the string 20170324 to the string 2017/03/24.
--Method 1: Use slices --Reference: http://stackoverflow.com/questions/4022827/insert-some-string-into-given-string-at-given-index-in-python --Method 2: Use datetime --Reference: http://qiita.com/shibainurou/items/0b0f8b0233c45fc163cd
test_python_170324k.py
orgstr = "20170324" # YYYYMMDD
print(orgstr)
# 1
newstr = orgstr[:4] + '/' + orgstr[4:6] + '/' + orgstr[6:]
print(newstr)
# 2
from datetime import datetime as dt
adt = dt.strptime(orgstr, '%Y%m%d')
newstr = adt.strftime('%Y/%m/%d')
print(newstr)
result
$ python test_python_170324k.py
20170324
2017/03/24
2017/03/24
At first I was thinking about insert, but http://stackoverflow.com/questions/4022827/insert-some-string-into-given-string-at-given-index-in-python according to
answered Oct 26 '10 at 12:02 bgporter An important point that often bites new Python programmers but the other posters haven't made explicit is that strings in Python are immutable -- you can't ever modify them in place
And that.
Recommended Posts