Python学习之问题记录

一、__init__.py的作用

当需要加载自定义的目录下所有文件时,让该目录识别为package,则目录下需要有__init__.py 文件,另外在模糊加载时,也需要定义 例如: __all__ = [“Pack1Class”,”Pack1Class1″], 总结如下:

Python中package的标识,不能删除

定义__all__用来模糊导入

编写Python代码(不建议在__init__中写python模块,可以在包中在创建另外的模块来写,尽量保证__init__.py简单)

二、python中时间差中seconds和total_seconds

在python中经常会用到计算两个时间差,两个日期类型进行相减可以获取到时间差

经常会使用seconds来获取,其实seconds获取的是时间差的秒数,遗漏了天

seconds是获取时间部分的差值,而total_seconds()是获取两个时间之间的总差

import datetime

t1 = datetime.datetime.strptime("2016-08-24 10:30:00", "%Y-%m-%d %H:%M:%S")
t2 = datetime.datetime.strptime("2016-08-24 12:30:00", "%Y-%m-%d %H:%M:%S")

interval_time = (t2 - t1).seconds  # 输入的结果:7200
total_interval_time = (t2 - t1).total_seconds() # 输出结果也是: 7200

# 换一个方式
t1 = datetime.datetime.strptime("2016-08-24 10:30:00", "%Y-%m-%d %H:%M:%S")
t2 = datetime.datetime.strptime("2016-08-27 12:30:00", "%Y-%m-%d %H:%M:%S")

interval_time = (t2 – t1).seconds # 输入的结果:7200 total_interval_time = (t2 – t1).total_seconds() # 输出结果也是: 266400

三、 遍历数组获取索引下标

for i, one_js in enumerate(js_actions):

四、Python出现’ascii’ codec can’t encode characters…的解决方法

第一步,别忘了给顶行加上:
# -*- coding: utf-8 -*-
第二步,重新载入SYS模块并设置uft-8

         import sys
         reload(sys)
         sys.setdefaultencoding(‘utf-8’)

You May Also Like

About the Author: daidai5771

0 Comments

  1. Sparkdex ai routing. Sparkdex AI Routing is a cutting-edge technology that revolutionizes the way we navigate and plan our routes. By harnessing the power of artificial intelligence, Sparkdex AI Routing is able to analyze vast amounts of data in real-time to provide users with the most efficient and optimal route for their journey. One of the key features of Sparkdex AI Routing is its ability to adapt to changing conditions on the fly. Whether it’s traffic congestion, road closures, or accidents, Sparkdex sparkdex.financial AI Routing can quickly reroute users to ensure they reach their destination as quickly as possible. Another key benefit of Sparkdex AI Routing is its ability to personalize routes based on individual preferences. By analyzing a user’s historical data and preferences, Sparkdex AI Routing can suggest routes that align with their preferences, such as avoiding highways or taking scenic routes. In addition to providing efficient routes, Sparkdex AI Routing also prioritizes safety. By taking into account factors such as weather conditions, time of day, and road conditions, Sparkdex AI Routing can suggest routes that are not only efficient but also safe for users. Furthermore, Sparkdex AI Routing is constantly learning and improving. By analyzing user feedback and data, Sparkdex AI Routing is able to continuously optimize its algorithms to provide even better routes for users. Overall, Sparkdex AI Routing is a game-changer in the world of navigation. Its ability to analyze data in real-time, adapt to changing conditions, personalize routes, prioritize safety, and continuously improve makes it the go-to choice for anyone looking for the most efficient and optimal route for their journey. Whether you’re a daily commuter, a road trip enthusiast, or a delivery driver, Sparkdex AI Routing has you covered. So next time you’re planning a journey, why not give Sparkdex AI Routing a try? You’ll be amazed at how much time and stress you can save by letting artificial intelligence do the heavy lifting for you. With Sparkdex AI Routing, navigating from point A to point B has never been easier or more efficient.

发表评论

电子邮件地址不会被公开。 必填项已用*标注