Maison  >  Article  >  développement back-end  >  软件工程惯用代码库 -- Python 篇

软件工程惯用代码库 -- Python 篇

高洛峰
高洛峰original
2017-02-17 11:13:071476parcourir

写这个主要是不想让自己老去找以前写过的旧代码,既劳心又劳力,还容易让人产生厌烦情绪。所以汇总一下都放在这边好了。

parse string into time object and calculate the interval (in seconds)

# 计算两个时间戳的差值import timefrom datetime import datetime

fmt = "%Y-%m-%dT%H:%M:%S.%fZ" #2016-02-18T12:22:22.123456Ztime1_obj = datetime.strptime(time1, fmt)
time2_obj = datetime.strptime(time2, fmt)
time_interval = int(time.mktime(time2_obj.timetuple()) - time.mktime(time1_obj.timetuple()))

call shell command in Python script

# Python 和 Shell 的结合在实际科研很有用
import subprocess
subprocess.call(["ls", "-l"], cwd=".")

read xml from file_path

import xml.etree.El
ementTree as ETtree = ET.parse("test.xml")
root = tree.getroot()
# apply dom operations on root

更多软件工程惯用代码库 -- Python 篇相关文章请关注PHP中文网!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:Python列表生成式 Article suivant:Python之Dict和Set类型