Fundamentals 5 min read

Simplify Python Date Strings: From Complex Code to Clean Formatting

This article walks through converting a high‑precision timestamp like "2022-03-25 08:00:00.000000000" into a readable Chinese date format using Python's datetime module, offering simpler alternatives to cumbersome string‑splitting tricks.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Simplify Python Date Strings: From Complex Code to Clean Formatting

Introduction

Hello, I’m PiPi. In a Python group a member asked how to turn the string 2022-03-25 08:00:00.000000000 into the format "2022年3月25日8时".

Initial Attempt

The original solution used multiple split calls and string concatenation, which was hard to read:

x = '2022-03-25 08:00:00.000000000'
result = x.split()[0].split('-')[0] + '年' + str(int(x.split()[0].split('-')[1])) + '月' + x.split()[0].split('-')[2] + '日' + str(int(x.split()[1].split(':')[0])) + '时'

While it works, it is overly complex.

Excel Shortcut (Reference)

One participant showed an Excel formula that achieves the same result: =TEXT(--LEFT(A1,19),"e年m月d日h时") This is not usable in code but demonstrates the desired output.

Python Solution

A concise Python approach uses datetime.strptime to parse the string and strftime to format it:

from datetime import datetime

date_str = '2022-03-25 08:00:00.000000000'
# Remove the trailing nanoseconds
date_str = date_str.split('.')[0]

dt = datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
formatted = dt.strftime('%Y年%m月%d日%H时')
print(formatted)  # 2022年03月25日08时

The key is trimming the extra nanosecond part before parsing; otherwise strptime raises an error.

Additional Tips

Other contributors suggested variations, but the core idea remains: preprocess the string to remove unnecessary precision, then use datetime for reliable conversion.

Conclusion

The article demonstrates several ways to handle date‑time strings in Python, emphasizing a clean, maintainable solution with the standard library, which can replace the original cumbersome manual splitting method.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythondatetimepandasString ManipulationDate Formatting
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.