python日常

1 utf8 编码

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

if __name__ == '__main__':

2 遍历文件夹

for root,dirs,files in os.walk(img_path):
    for file in files:
        # 文件名
        print(file)
        # 全路径
        print(os.path.join(root, file))

3 写文件

file_write = open(filename, "w")
line = "abc\ndef"
file_write.write(line)

4 读文件

file = open(r'c:\x.txt','r')
for i,line in enumerate(file):
    print('第'+str(i)+'行:'+line)
file.close()

5 PIL 图片操作

  • 打开图片:

    from PIL import Image
    img = Image.open("路径") 
    # 若图片为彩色图像,open出来会是RGB格式
    # 若图片为灰度图像,open出来会是L格式
    
  • 改变图片大小

    reIm = img.resize((28,28), Image.ANTIALIAS) 
    # 该成28*28大小的,ANTIALIAS表示抗锯齿转换,也是高质量的转换
    
  • 改变图片格式

    (img原格式为打开时的格式,为RGB格式,每个点有三色,查看一个点的用函数 img.getpixel((0,0))  )
    img1 = img.convert("1") # 三色转换为0或255
    img2 = img.convert("L") # 三色转换为对应的平均值,即灰度
    img3 = img.convert("P") #  三色转换为对用调色板上的索引,恢复成RGB后,会出现失真
    img4 = img.convert("RGBA") # 三色不变,但会增加一项--透明度,恢复成RGB后三色不变
    
  • 显示图片

    img.show() 
    
  • 保存图片

    img.save('d:/dog.jpg')
    

6 类

class fun():
	__DATA = 0 #以‘__’开头的变量为私有变量,外部不能用xx.__DATA来访问
	data = 1 #公共变量,外部可以用xx.data来访问。这里定义等效于在下面的函数中用self.data定义

	def __init__(self, a, b,*arg_list):  #self参数为必要参数,其他为可选参数,其中*arg_list是变量数组
		self.__DATA = a
		self.data = b
		for arg in arg_list: # arg_list可以用局部变量遍历来获取
			print(arg)
		return
		
	def test(self):
		return

7 变量的命名规则

xx共有变量/方法
_xx私有变量/方法,外部(即其他的.py文件)不能访问,类对象和子类可以访问
__xx私有变量/方法,外部(即其他的.py文件)不能访问
xx系统定义的名字
xx_避免于python关键词发生冲突

8 记录运行时间

from time import time
t0 = time()
# 这里添加需要记录时间的代码段

print("use time:",
round(time()-t0, 3), "s")
# 其实time()就是获取当前时间

9 round() 四舍五入

round( x [, n]  )
# 其中n为精度,n=3表示保存3为小数

9 导入其他文件夹下的模块

import sys
sys.path.append("文件夹路径")

10 判断数据类型

type(xxx) # 查看xxx的数据类型
isinstance(xxx, int) # 判断xxx是否为int型

11 增/删(list、numpy)的元素

11.1 list类型:

num = [1,2,3,4,5,6]
max(num) # 最大值
i = num.index() # 根据值获取索引
del num[索引] # 删除指定(索引)的元素
num.remove() # 删除指定(值)的元素
num.pop(索引) # 删除指定元素,若不填索引,则默认最后一个数
xxx.insert(0,) #在指定位置添加元素

11.2 numpy类型:

num = np.array([1,2,3,4,5,6])
i = np.argmax(num) # 获取最大值的索引
np.delete(num, 1, axis=0) # 第二个参数为索引,第三个为横/纵方向
np.insert(num, 1, [xxxx], axis=0) #同上,第三个参数为添加的值
np.append(num, [[xxx]], axid=0) # 追加。

12 zip函数将数组重组成元组

a = [1,2,3]
b = [4,5,6]
c = [7,8,9]

n = zip(a,b,c) # 生成元组(1,4,7), (2,5,8), (3,6,9)
m = zip(*n) #还原之前的组合元素,还原后是元组类型。还原之前,n不要用print打印,否则会破坏n

13 字符串匹配

13.1 re.search

从字符串中查找指定字符串,找到则返回第一个匹配,否则返回None。

import re

line = "xxxxxxabcxxxxxxx"
pattern = "abc"
match = re.search(pattern, line)

13.2 re.match

从起始位置来匹配两个字符串。

import re

line = "xxxxxxabcxxxxxxx"
pattern = "abc"
match = re.match(pattern, line)

13.3 re.sub

用于替换字符串中的匹配项。

import re
 
line="this hdr-biz model args= server"
patt=r'args='
name = re.sub(patt, "", line)

13.4 re.findall

用正则表达式查找的所有子串

import re
 
line="this hdr-biz model args= server"
patt=r'server'
pattern = re.compile(patt)
result = pattern.findall(line)

import re
 
it = re.finditer(r"\d+","12a32bc43jf3")
for match in it:
      print (match.group() )

14 文件移动、复制

import os, shutil

def moveFile(src, dst):
	if not os.path.isfile(src):
		print("%s not exist!" % {src})
	else:
		fpath, fname = os.path.split(dst)
		if not os.path.exists(fpath):
			os.makedirs(fpath)
		shutil.move(src, dst)
		
def copyFile(src, dst):
	if not os.path.isfile(src):
		print("%s not exist!" % {src})
	else:
		fpath, fname = os.path.split(dst)
		if not os.path.exists(fpath):
			os.makedirs(fpath)
		shutil.copyfile(src, dst)

if __name__ == "__main__":
	path1 = "./aaa/xxx.txt"
	path2 = "./bbb/xxx.txt"
	copyFile(path1, path2)

15 随机抽取列表的元素

  • 随机抽取列表的一个元素
    from random import choice
    foo = ['a', 'b', 'c', 'd', 'e']
    print choice(foo)
    
  • 随机抽取指定个数的元素
    import random
    list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
    random.seed(10) #设置种子使得每次抽样结果相同
    slice = random.sample(list, 5)  #从list中随机获取5个元素,作为一个片断返回  
    print slice  
    

16 int 格式化为固定长度的 str

format(123, '05d')
# 将转换为 str 格式的 "00123"

17 取消打印矩阵的省略号

在 python 打印尺寸比较大的矩阵时,中间的数据会自动用省略号代替,如果要取消省略号全部打印出来,设置一下:

import pandas as pd

# 核心代码,设置显示的最大列、宽等参数,消掉打印不完全中间的省略号
pd.set_option('display.max_columns', 1000)
pd.set_option('display.width', 1000)
pd.set_option('display.max_colwidth', 1000)

18 字符串对齐

test = 'hello world'
print(test.ljust(20))  #左对齐
print(test.ljust(20, '*'))
print(test.rjust(20, '*')) #右对齐
print(test.center(20, '*'))
print(test.center(20))#居中

19 字典排序

用key排序
d = {'d1':2, 'd2':4, 'd4':1,'d3':3,}
for k in sorted(d):
  print(k,d[k])
d1 2
d2 4
d3 3
d4 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值