Python / datetime> Implementation to convert YYYYMMDD format to YYYY / MM / DD

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

Python / datetime> Implementation to convert YYYYMMDD format to YYYY / MM / DD
Convert Python date types to RFC822 format
Convert "number" of excel date to python datetime
Convert strings to character-by-character list format with python
python> datetime> yyyy / mm / dd, hh: nn: ss format print> today = datetime.datetime.today () / print today.strftime ("% Y /% m /% d,% H:% M:" % S ")
Convert / return class object to JSON format in Python
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
[python] Convert date to string
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
2015-12-26 python2> datetime> Implementation to take the difference in seconds from two ISO format datetime strings> Use .seconds ()
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Convert python 3.x code to python 2.x
Determine the date and time format in Python and convert to Unixtime
Convert markdown to PDF in Python
Workflow to convert formula (image) to python
Convert list to DataFrame with python
python datetime format quick reference table
Python> list> Convert double list to single list
[Python] Convert natural numbers to ordinal numbers
Convert decimal numbers to n-ary numbers [python]
Python> tuple> Convert double tuple to single tuple
Convert XML document stored in XML database (BaseX) to CSV format (using Python)
I made a script in python to convert .md files to Scrapbox format
Preprocessing with Python. Convert Nico Nico Douga tag search results to CSV format
Convert xml format data to txt format data (yolov3)
Convert memo at once with Python 2to3
How to easily convert format from Markdown
Convert Python> two value sequence to dictionary
[Python] How to convert a 2D list to a 1D list
How to convert Python to an exe file
Convert matplotlib graphs to emf file format
[Python] Convert csv file delimiters to tab delimiters
Convert Hiragana to Romaji with Python (Beta)
Convert from katakana to vowel kana [python]
Rename YYYYMMDD format files to serial numbers
Convert FX 1-minute data to 5-minute data with Python
python> Convert tuple to list> aList = list (pi_tuple)
Convert PDF attached to email to text format
Convert HEIC files to PNG files with Python
Convert Chinese numerals to Arabic numerals with Python
How to convert DateTimeField format in Django
Convert from Markdown to HTML in Python
Convert absolute URLs to relative URLs in Python
Sample to convert image to Wavelet with Python
Python datetime How to calculate dates and convert strings strftime, strptime [Definitive Edition]