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代码大全简单图解
|
11天前
|
JSON 人工智能 数据挖掘
Python零基础入门必背代码大全
Python零基础入门必背代码大全
15 1
|
10天前
|
JSON 数据格式 索引
Python之巅:探索50个代码大全
Python之巅:探索50个代码大全
8 0
|
2月前
|
数据采集 机器学习/深度学习 人工智能
python代码大全
python代码大全
42 0
|
3天前
|
存储 SQL 数据可视化
Python 金融编程第二版(二)(4)
Python 金融编程第二版(二)
11 1
|
3天前
|
存储 分布式计算 数据可视化
Python 金融编程第二版(四)(2)
Python 金融编程第二版(四)
13 0
|
3天前
|
存储 SQL 数据可视化
Python 金融编程第二版(四)(1)
Python 金融编程第二版(四)
9 0
|
3天前
|
数据挖掘 索引 Python
Python 金融编程第二版(二)(5)
Python 金融编程第二版(二)
7 0
|
3天前
|
数据可视化 Python
Python 金融编程第二版(三)(4)
Python 金融编程第二版(三)
12 2

相关实验场景

更多