(资料图)
目录
1. 新建文件夹2. 后台运行并保存log3. 文件读取4. 字符串判断5. 统计list元素出现次数6. timestamp 转换标准时间7. 排序8. 文件路径获取9. 同一行刷新打印10. PIL resize比opencv更清晰11. base64转opencv1. 新建文件夹
if not os.path.exists(feature_dir):
os.makedirs(feature_dir)
2. 后台运行并保存log
nohup python -u test.py > test.log 2>&1 & #最后的&表示后台运行 #2 输出错误信息到提示符窗口 #1 表示输出信息到提示符窗口, 1前面的&注意添加, 否则还会创建一个名为1的文件 #最后会把日志文件输出到test.log文件 #查看 tail -f test.log#如果要实时查看日志文件使用命令 cat test.log#查看全部输出使用命令
3. 文件读取
###1.python
#读写txt
with open(r"./data/user_dict.txt","r",encoding="utf-8") as f:
data = f.readlines()
#追加模式
with open(r"./data/user_dict.txt","a",encoding="utf-8") as f:
t = "你好"
f.write("\n"+t)
#按行读取tsv / 内存大可以直接.readlines()
with open("./data/train.tsv",encoding = "utf-8") as file:
line = file.readline()
limit = 0
while line and limit<10:
print(line)
limit+=1
line = file.readline()
###2.json 存储dict
x = {..}
#save
with open(r"./x.json","w") as f:
json.dump(x, f, ensure_ascii=False) #单行
print("done")
## 格式化
with open(r"result.json", "w") as f:
json.dump(res, f, ensure_ascii=False, indent=4)
#read
with open(r"./x.json","r") as f:
x = json.loads(f.readlines()[0])
#读取格式化后的多行json
with open(r"./x.json","r") as f:
x = json.load(f)
###3.numpy 存储list
x = [x,]
np.save("./././x.npy",x)
x = np.load(r"./././x.npy")
###4.pandas
#read xlsx
data = pd.read_excel(r"xxxx.xlsx","Sheet1")
#dict to df
result = {x:1,y:2,..}
df = pd.DataFrame(list(result.items()), columns=["key","value"])
#save df
df.to_csv(r"./result.csv", index=False,header=True)
#read
df = pd.read_csv(r"./result.csv",encoding = "gbk")
4. 字符串判断
s.islower() #判断是否所有字符小写 s.isupper() #判断是否所有字符大写 s.isalpha() #判断是否所有字符为字母 s.isalnum() #判断是否所有字符为字母或数字 s.isdigit() #判断是否所有字符为数字 s.istitle() #判断是否所有字符为首字母大写
5. 统计list元素出现次数
from collections import Counter
x = [1,2,3,2]
y= "1232"
Counter(x)
#>>Counter({2: 2, 1: 1, 3: 1}) #就是一个dict
Counter(y)
#>>Counter({"2": 2, "1": 1, "3": 1})
Counter("1232")["2"]
#>>2
6. timestamp 转换标准时间
# 把时间处理 以找到登陆时间
import time
def timestamp_datetime(value):
format = "%Y-%m-%d %H:%M:%S"
# value为传入的值为时间戳(整形),如:1332888820
value = time.localtime(value)
## 经过localtime转换后变成
## time.struct_time(tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, tm_wday=2, tm_yday=88, tm_isdst=0)
# 最后再经过strftime函数转换为正常日期格式。
dt = time.strftime(format, value)
return dt
def datetime_timestamp(dt):
#dt为字符串
#中间过程,一般都需要将字符串转化为时间数组
time.strptime(dt, "%Y-%m-%d %H:%M:%S")
## time.struct_time(tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, tm_wday=2, tm_yday=88, tm_isdst=-1)
#将"2012-03-28 06:53:40"转化为时间戳
s = time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S"))
return int(s)
d = datetime_timestamp("2015-03-30 16:38:20")
print(d)
s = timestamp_datetime(1427704700)
print(s)
7. 排序
#方法1.用List的成员函数sort进行排序,在本地进行排序,不返回副本 #方法2.用built-in函数sorted进行排序(从2.4开始),返回副本,原始输入不变 listX = [[1,4],[2,5],[3,3]] sorted(listX, key=lambda x : x[1]) #>>[[3, 3], [1, 4], [2, 5]] ### 两个list按同意顺序排序 list1 = [1, 2, 3, 4, 15, 6] list2 = ["a", "b", "c", "d", "e", "f"] c = list(zip(list1,list2)) c.sort(reverse=True) #降序du list1[:],list2[:] = zip(*c) print(list1,list2)
8. 文件路径获取
path1 = os.getcwd() #最外层执行的main.py的路径 path2 = os.path.dirname(os.path.realpath(__file__)) #当前py文件的绝对路径
9. 同一行刷新打印
print("\r",object,end="",flush=True)
#e.g.
for i,img_name in enumerate(img_names):
print("\r",str(i)+"/"+str(len(img_names)),end="",flush=True)10. PIL resize比opencv更清晰
img = cv2.imread("000000000113_0.jpg")
img = Image.fromarray(img)
img = img.resize((192,192))
img = np.array(img)
11. base64转opencv
def imgToBase64(img_array):
# 传入图片为RGB格式numpy矩阵,传出的base64也是通过RGB的编码
img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR) #RGB2BGR,用于cv2编码
encode_image = cv2.imencode(".jpg", img_array)[1] #用cv2压缩/编码,转为一维数组
byte_data = encode_image.tobytes() #转换为二进制
base64_str = base64.b64encode(byte_data).decode("ascii") #转换为base64
return base64_str
def base64ToImg(base64_str):
# 传入为RGB格式下的base64,传出为RGB格式的numpy矩阵
byte_data = base64.b64decode(base64_str)#将base64转换为二进制
encode_image = np.asarray(bytearray(byte_data), dtype="uint8")# 二进制转换为一维数组
img_array = cv2.imdecode(encode_image, cv2.IMREAD_COLOR)# 用cv2解码为三通道矩阵
img_array = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB)# BGR2RGB
return img_array到此这篇关于Python中常用功能的实现代码分享的文章就介绍到这了,更多相关Python常用功能内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
关键词:
-
当前要闻:iphone13promax充电多久满(iphone13promax充电多少时间)iPhone13的充电速度是多快,充满电的时间是多久,这里我们来了解下iPho -
校友会2023中国大学法学实力排名,中国人民大学第一,北大第二-天天新消息中国法学门类哪所大学办学实力最强、办学水平最高?哪些大学跻身校友会 -
汉鼎一号十二米鱼杆价格_十二米长水上貂鱼杆价格 今日讯1、给出的这些数据基本上没有什么用。2、你看下上千元的鱼竿和百元左右 -
出门问问IPO:第一大客户及产品频繁变更 创始人高喊“不差钱”为何还要融资_播报出门问问IPO:第一大客户及产品频繁变更创始人高喊“不差钱”为何还要 -
2023年南宁良庆区小学报名时间 天天快讯报名时间及方式:(一)网上填报信息。2023年7月3日8:30至7月9日17:30,
-
当前要闻:iphone13promax充电多久满(iphone13promax充电多少时间)
2023-07-05 07:12:53
-
校友会2023中国大学法学实力排名,中国人民大学第一,北大第二-天天新消息
2023-07-05 07:10:34
-
汉鼎一号十二米鱼杆价格_十二米长水上貂鱼杆价格 今日讯
2023-07-05 06:13:56
-
出门问问IPO:第一大客户及产品频繁变更 创始人高喊“不差钱”为何还要融资_播报
2023-07-05 05:50:45
-
2023年南宁良庆区小学报名时间 天天快讯
2023-07-05 05:26:24
即时推荐


















营业执照公示信息