python常用代码大全分享

简介: python常用代码大全分享

def getFileList(dir,Filelist, ext=None):

  1. newDir = dir
  2. if os.path.isfile(dir):
  3. if ext is None:
  4. Filelist.append(dir)
  5. else:
  6. if ext in dir[-3:]:
  7. Filelist.append(dir)
  8. elif os.path.isdir(dir):
  9. for s in os.listdir(dir):
  10. newDir=os.path.join(dir,s)
  11. getFileList(newDir, Filelist, ext)
  12. return Filelist
      //代码效果参考:http://www.ezhiqi.com/zx/art_5352.html

2.对指定数组进行等分,可以用于多线程程序
复制代码
1 def div_list(ls,n):
2 result = []
3 cut = int(len(ls)/n)
4 if cut == 0:
5 ls = [[x] for x in ls]
6 none_array = [[] for i in range(0, n-len(ls))]
7 return ls+none_array
8 for i in range(0, n-1):
9 result.append(ls[cuti:cut(1+i)])
10 result.append(ls[cut*(n-1):len(ls)])
11 return result
3.用csv文件处理数据集
1 import threading
2 import os
3 from PIL import Image
4 import math
5 import pandas as pd
6 from tqdm import tqdm
//代码效果参考:http://www.ezhiqi.com/bx/art_6717.html

8 image_path = "/data0/Manually_Annotated_Images/"
9 save_path = "/home/frank/affectNet/val/"
10 filename = "/data0/validation.csv"
11
12 def savePic(subdata):
13 print("thread %s is running..." %threading.current_thread().name)
14 for index, row in tqdm(subdata.iterrows()):
15 try:
16 imgPath = image_path + row['subDirectory_filePath']
17 image = Image.open(imgPath)
18 classes = row['expression']
19 event = row['subDirectory_filePath'].split('/')[-1]
20 if not os.path.exists(save_path + classes):
21 os.makedirs(save_path + classes)
22 image.save(save_path+classes+'/'+event)
23 except:
24 pass
25 print("thread %s is ended....." %threading.current_thread().name)
26
27
28
29
30 def splitdf(df,num):
31 linenum = math.floor(len(df)/num)
32 pdlist = []
33 for i in range(num):
34
35 pd1 = df[ilinenum:(i+1)linenum]
36 pdlist.append(pd1)
37 # print(len(pd1))
38 pd1 = df[(num-1)*linenum:len(df)]
39 pdlist.append(pd1)
40 return pdlist
41
42 data = pd.read_csv(filename)
43 data = data.applymap(str)
44 subData = splitdf(data,32)
45
46 th = []
47 for i in range(32):
48 t = threading.Thread(target=savePic,args = (subData[i],))
49 t.start()
50 th.append(t)
//代码效果参考:http://www.ezhiqi.com/bx/art_2649.html

51
52 for t in th:
53 t.join()
54
55
56
57 print("saving is success!")

相关文章
|
2月前
|
Java Python
python代码大全
python代码大全(小白篇)
|
2月前
|
Java Python
python代码大全简单图解
python代码大全简单图解
|
20天前
|
JSON 人工智能 数据挖掘
Python零基础入门必背代码大全
Python零基础入门必背代码大全
19 1
|
18天前
|
JSON 数据格式 索引
Python之巅:探索50个代码大全
Python之巅:探索50个代码大全
9 0
|
2月前
|
数据采集 机器学习/深度学习 人工智能
python代码大全
python代码大全
42 0
|
6天前
|
安全 Python
告别低效编程!Python线程与进程并发技术详解,让你的代码飞起来!
【7月更文挑战第9天】Python并发编程提升效率:**理解并发与并行,线程借助`threading`模块处理IO密集型任务,受限于GIL;进程用`multiprocessing`实现并行,绕过GIL限制。示例展示线程和进程创建及同步。选择合适模型,注意线程安全,利用多核,优化性能,实现高效并发编程。
20 3
|
8天前
|
开发者 Python
Python元类实战:打造你的专属编程魔法,让代码随心所欲变化
【7月更文挑战第7天】Python的元类是编程的变形师,用于创建类的“类”,赋予代码在构建时的变形能力。
30 1
|
9天前
|
设计模式 存储 Python
Python元类大揭秘:从理解到应用,一步步构建你的编程帝国
【7月更文挑战第6天】Python元类是创建类的对象的基石,允许控制类的生成过程。通过自定义元类,可在类定义时动态添加方法或改变行为。
16 0
|
6天前
|
数据采集 大数据 数据安全/隐私保护
Python编程:如何有效等待套接字的读取与关闭
Python网络编程中,套接字事件处理至关重要。利用`selectors`模块和代理IP能增强程序的稳定性和可靠性。代码示例展示了如何通过代理连接目标服务器,注册套接字的读写事件并高效处理。在代理IP配置、连接创建、事件循环及回调函数中,实现了数据收发与连接管理,有效应对网络爬虫或聊天应用的需求,同时保护了真实IP。
Python编程:如何有效等待套接字的读取与关闭
|
1天前
|
数据挖掘 开发者 Python
如何自学Python编程?
【7月更文挑战第14天】如何自学Python编程?
16 4

相关实验场景

更多