关于编程的知识

【知识分享】Python常用设置

关键词: 编程 Python

编辑者:爱智智能                    编辑日期:2020年12月11日                    浏览量:0


爱智智能为您介绍Python常用设置。

 

国内源

清华:https://pypi.tuna.tsinghua.edu.cn/simple

阿里云:http://mirrors.aliyun.com/pypi/simple/

中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/

华中理工大学:http://pypi.hustunique.com/

山东理工大学:http://pypi.sdutlinux.org/

豆瓣:http://pypi.douban.com/simple/

note:新版ubuntu要求使用https源,要注意。

例如:pip3 install -i https://pypi.doubanio.com/simple/ 包名

 

安装库

pip install tensorflow==2.3.1

pip install --upgrade 库名称

pip install https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-2.3.1-cp37-cp37m-win_amd64.whl

conda install cudatoolkit=10.0.0

conda install tensorflow==1.1.0 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/win-64/ -y

批量安装:路径/python.exe -m pip install -r 路径\requirements.txt

 

字符串与时间戳的相互转换

# 时间字符串转换为时间戳

a = "2011-10-10 22:40:00"

# 将其转换为时间数组

import time

timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")

# 转换为时间戳:

timeStamp = int(time.mktime(timeArray))

# 将时间戳转换成时间字符串

timeStamp = 1381419600

timeArray = time.localtime(timeStamp)

otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)

# 获取当前时间并转换为指定日期格式

import time

# 获得当前时间时间戳

now = int(time.time()) # ->这是时间戳

# 转换为其他日期格式, 如: "%Y-%m-%d %H:%M:%S"

timeArray = time.localtime(timeStamp)

otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)

# 缩写代表含义

%a 星期的简写。如 星期三为Web

%A 星期的全写。如 星期三为Wednesday

%b 月份的简写。如4月份为Apr

%B 月份的全写。如4月份为April

%c: 日期时间的字符串表示。(如: 04/07/10 10:43:39)

%d: 日在这个月中的天数(是这个月的第几天)

%f: 微秒(范围[0,999999])

%H: 小时(24小时制,[0, 23])

%I: 小时(12小时制,[0, 11])

%j: 日在年中的天数 [001,366](是当年的第几天)

%m: 月份([01,12])

%M: 分钟([00,59])

%p: AM或者PM

%S: 秒(范围为[00,61],为什么不是[00, 59],参考python手册~_~)

%U: 周在当年的周数当年的第几周),星期天作为周的第一天

%w: 今天在这周的天数,范围为[0, 6],6表示星期天

%W: 周在当年的周数(是当年的第几周),星期一作为周的第一天

%x: 日期字符串(如:04/07/10)

%X: 时间字符串(如:10:43:39)

%y: 2个数字表示的年份

%Y: 4个数字表示的年份

%z: 与utc时间的间隔 (如果是本地时间,返回空字符串)

%Z: 时区名称(如果是本地时间,返回空字符串)

%%: %% => %

 

pycharm自动写入信息

路径:Pycharm——Preferences——Editor——File and Code Templates——Python Script

# -*- coding: utf-8 -*-

#!@Created on ${DATE}${TIME}

#!@author: Aismart

#!@email:vincent_aismart@163.com

#!@phone:13290185982

#!@website:www.aismart.vip

import time

start_all = time.perf_counter()

"""main code"""

all_time = end_all - start_all

print('总用时:', all_time,"秒即",round(round(all_time // 60)//60),"小时",round(round(all_time // 60)%60), '分', round(all_time % 60), '秒')

 

pycharm批量注释

选中要注释的代码

选中之后,同时按住ctrl键和/键,即可批量注释。

 

python运算符

%取余:返回除法的余数

/除:x除以y

//取整除:返回商的整数部分(向下取整)

**幂:返回x的y次幂

 

删除空格

strip():删除字符串前后的空白

lstrip():删除字符串前面(左边)的空白

rstrip():删除字符串后面(右边)的空白

s2 = 'i think it is a scarecrow'

s2.lstrip('itow'):删除左边的i、t、o、w字符

s2.rstrip('itow'):删除右边的i、t、o、w字符

s2.strip('itow'):删除两边的i、t、o、w字符

s2.replace(" ", ""):删除字符串中全部空格

\xa0 是不间断空白符  我们通常所用的空格是 \x20 ,是在标准ASCII可见字符 0x20~0x7e 范围内。而 \xa0 属于 latin1 (ISO/IEC_8859-1)中的扩展字符集字符,代表空白符nbsp(non-breaking space)。