Date formats बदलना - स्पष्ट तरीके से
आइए पिछले अभ्यास में दी गई तारीखों में से एक को देखें.
- Sale A: 4000 on 14/02/2018
हमने अलग-अलग date formats वाले datasets से डेटा पहचानने में मदद के लिए datetime लाइब्रेरी से day d, month m, और year y निकाले थे. लेकिन अगर हमें तारीख को किसी खास format में बदलना हो तो?
इस अभ्यास में आप Sale A को 14/02/2018 से Sale B के समान date format (यानी 14 February 2018) में बदलेंगे.
हम यह काम Python के built-in functions से आसानी से कर सकते हैं. याद रखें, किसी string को बाँटने के लिए हम .split() मेथड का उपयोग कर सकते हैं.
इस अभ्यास के लिए इनपुट, Sale A का datetime होगा.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Financial Forecasting
अभ्यास निर्देश
- एक वैरिएबल
dtबनाएँ जिसमें उस तारीख का string रूप हो जिसे आप नए format में बदलना चाहते हैं (यानी Sale A). - महीनों के लिए एक dictionary
mmबनाएँ, जो बताए कि कौन-सा month किस number से मेल खाता है, format{'number':'month name'}में. dtstring को/के आधार पर split करें और मानों कोday,monthऔरyearवैरिएबल्स में असाइन करें.- दिए गए month के लिए dictionary का उपयोग करते हुए strings को जोड़कर (concatenate करके) परिणाम नए format में प्रिंट करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Set the variable for the datetime to convert
dt = '____'
# Create the dictionary for the month values
mm = {'01': '____', '____': 'February', '____': '____'}
# Split the dt string into the different parts
____, ____, year = dt.split('____')
# Print the concatenated date string
print(____ + ' ' + ____[____] + ' ' + ____)