Environnement d'exploitation
Xeon E5-2620 v4 (8 noyaux) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 et ses-devel
mpich.x86_64 3.1-5.el6 et ses-devel
gcc version 4.4.7 (Et gfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.Utilisez 1.
Python 3.6.0 on virtualenv
Je souhaite convertir la chaîne de caractères 20170324 en chaîne de caractères 2017/03/24.
--Méthode 1: utiliser des tranches --Référence: http://stackoverflow.com/questions/4022827/insert-some-string-into-given-string-at-given-index-in-python --Méthode 2: utiliser datetime
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)
résultat
$ python test_python_170324k.py
20170324
2017/03/24
2017/03/24
Au début, je pensais à insérer, etc. http://stackoverflow.com/questions/4022827/insert-some-string-into-given-string-at-given-index-in-python selon
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
Et cela.
Recommended Posts