Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are some useful libraries in python?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, Xiaobian will bring you some useful libraries in python. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.

Time Library-arrow Use Background

Date and time processing is ubiquitous in real-world scenarios, so it has become an essential module in programming languages, and Python is no exception. But do you know how many related modules there are in Python? datetime, time, calendar, dateutil, pytz, etc. Do you know how many data types there are? date, time, datetime, tzinfo, timedelta, etc.

The blogger encountered a need to count the data of recent months in a request. The front end sent me the current timestamp. I had to push forward three months, or four months, or half a year through the timestamp. If I could only add or subtract time, it would become very troublesome, so I thought of using python's time library---arrow.

Before introducing the time library, let's take a look at how Python's various time formats are converted

In any case, you still have to be proficient in these modules and API operations. It doesn't matter if you can't remember them. At least you have to manually type them a few times. When you encounter them next time, you should be able to quickly locate what a certain method of a certain class is used for and how to use it.

But today I want to strongly amway to give you this time-date library: Arrow. It was inspired by the requests library. Hide everything behind you and leave you with the for humans interface. Keep it simple and stupid.

install arrowpip install arroarrow using

acquires current time

Get different time formats

vernier shift

This is an attribute that I think is best used in arrow. It allows you to move time forward or backward at will. It can basically satisfy all your operations on time calculation. Use it as soon as possible.

backdating

forward reckoning

humanize

humanize method is a readable line string form expressed as "how long ago" relative to the current time. The default is English format. Specify locale to display the corresponding language format.

format

Get arrow object

The get method is the best way to get an arrow object

#Without parameters, equivalent to utcnow()>> arrow.get()#Accepts timestamp parameters>> arrow.get (1535113845)#Accept a datetime object>> arrow.get (datetime(2018,8,24))#Receive a date object>>> from datetime import date>> arrow.get (date(2018,7,24))#receive date format string>>> arrow.get ("2018-08-11 12:30:56")#Receive date string and specify format>> arrow.get ("18-08-11 12:30:56", "YY-MM-DD HH:mm:ss") type conversion and time zone modification of arrow #string to time print (arrow.get('2017-10-28T00:00:00+0800'))# print (arrow.get ('2017 -10- 28','Y-MM-DD')) #String to Time (Not recommended, no time zone) print (arrow.get('2017-10-28', 'Y-MM-DD', tzinfo='local')) #(Recommended) print (arrow.get ('2017 -10- 28','Y-MM-DD', tzinfo='Asia/Shanghai')) #This can also print (arrow.get('2017-10-28 05:30:30', 'YYYY-MM-DD HH:mm: ss', tzinfo=' local')) #(recommended) print ('------------')#timestamp print (local.timestamp) #timestamp print (arrow.get ('1509120000 ', tzinfo ='local')) #timestamp string, time converted to this time zone #time converted to string, output (formatted) print (local.format("YYYY-MM-DD"))print (local.format("YYYY-MM-DD HH:mm:ss"))print (local.replace(minutes=-1).humanize (locale ='zh ') #Localized personality time phrase: just now, 1 minute ago, 1 day ago, etc.(zh_tw More language support, go to see arrow/locales.py) The above is what some of the Python libraries that are easy to use are shared by Xiaobian for everyone. If you happen to have similar doubts, you may wish to refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report