1. 新建文件夾
1if not os.path.exists(feature_dir):
2 os.makedirs(feature_dir)
2. 后臺(tái)運(yùn)行并保存log
1nohup python -u test.py > test.log 2>&1 &
2
3
4
5
6
7
8tail -f test.log
9cat test.log
3. 文件讀取
1
2
3with open(r'./data/user_dict.txt','r',encoding='utf-8') as f:
4 data = f.readlines()
5
6with open(r'./data/user_dict.txt','a',encoding='utf-8') as f:
7 t = '你好'
8 f.write('\\n'+t)
9
10
11with open('./data/train.tsv',encoding = 'utf-8') as file:
12 line = file.readline()
13 limit = 0
14 while line and limit<10:
15 print(line)
16 limit+=1
17 line = file.readline()
18
19
20x = {..}
21
22with open(r"./x.json",'w') as f:
23 json.dump(x, f, ensure_ascii=False)
24print('done')
25
26with open(r"result.json", 'w') as f:
27 json.dump(res, f, ensure_ascii=False, indent=4)
28
29
30with open(r"./x.json",'r') as f:
31 x = json.loads(f.readlines()[0])
32
33with open(r"./x.json",'r') as f:
34 x = json.load(f)
35
36
37x = [x,]
38np.save("./././x.npy",x)
39x = np.load(r"./././x.npy")
40
41
42
43data = pd.read_excel(r'xxxx.xlsx','Sheet1')
44
45
46result = {x:1,y:2,..}
47df = pd.DataFrame(list(result.items()), columns=['key','value'])
48
49df.to_csv(r"./result.csv", index=False,header=True)
50
51df = pd.read_csv(r'./result.csv',encoding = 'gbk')
4. 字符串判斷
1s.islower() #判斷是否所有字符小寫
2s.isupper() #判斷是否所有字符大寫
3s.isalpha() #判斷是否所有字符為字母
4s.isalnum() #判斷是否所有字符為字母或數(shù)字
5s.isdigit() #判斷是否所有字符為數(shù)字
6s.istitle() #判斷是否所有字符為首字母大寫
5. 統(tǒng)計(jì)list元素出現(xiàn)次數(shù)
1from collections import Counter
2x = [1,2,3,2]
3y= '1232'
4Counter(x)
5#>>Counter({2: 2, 1: 1, 3: 1}) #就是一個(gè)dict
6Counter(y)
7#>>Counter({'2': 2, '1': 1, '3': 1})
8Counter('1232')['2']
9#>>2
6. timestamp 轉(zhuǎn)換標(biāo)準(zhǔn)時(shí)間
1# 把時(shí)間處理 以找到登陸時(shí)間
2import time
3def timestamp_datetime(value):
4 format = '%Y-%m-%d %H:%M:%S'
5 # value為傳入的值為時(shí)間戳(整形),如:1332888820
6 value = time.localtime(value)
7 ## 經(jīng)過localtime轉(zhuǎn)換后變成
8 ## 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)
9 # 最后再經(jīng)過strftime函數(shù)轉(zhuǎn)換為正常日期格式。
10 dt = time.strftime(format, value)
11 return dt
12def datetime_timestamp(dt):
13 #dt為字符串
14 #中間過程,一般都需要將字符串轉(zhuǎn)化為時(shí)間數(shù)組
15 time.strptime(dt, '%Y-%m-%d %H:%M:%S')
16 ## 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)
17 #將"2012-03-28 06:53:40"轉(zhuǎn)化為時(shí)間戳
18 s = time.mktime(time.strptime(dt, '%Y-%m-%d %H:%M:%S'))
19 return int(s)
20
21d = datetime_timestamp('2015-03-30 16:38:20')
22print(d)
23s = timestamp_datetime(1427704700)
24print(s)
7. 排序
1#方法1.用List的成員函數(shù)sort進(jìn)行排序,在本地進(jìn)行排序,不返回副本
2#方法2.用built-in函數(shù)sorted進(jìn)行排序(從2.4開始),返回副本,原始輸入不變
3listX = [[1,4],[2,5],[3,3]]
4sorted(listX, key=lambda x : x[1])
5#>>[[3, 3], [1, 4], [2, 5]]
6
7### 兩個(gè)list按同意順序排序
8list1 = [1, 2, 3, 4, 15, 6]
9list2 = ['a', 'b', 'c', 'd', 'e', 'f']
10c = list(zip(list1,list2))
11c.sort(reverse=True) #降序du
12list1[:],list2[:] = zip(*c)
13print(list1,list2)
8. 文件路徑獲取
1path1 = os.getcwd()
2path2 = os.path.dirname(os.path.realpath(__file__))
9. 同一行刷新打印
1print("\\r",object,end="",flush=True)
2
3
4for i,img_name in enumerate(img_names):
5 print("\\r",str(i)+"/"+str(len(img_names)),end="",flush=True)
10. PIL resize比opencv更清晰
1img = cv2.imread("000000000113_0.jpg")
2img = Image.fromarray(img)
3img = img.resize((192,192))
4img = np.array(img)
11. base64轉(zhuǎn)opencv
1def imgToBase64(img_array):
2 # 傳入圖片為RGB格式numpy矩陣,傳出的base64也是通過RGB的編碼
3 img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR) #RGB2BGR,用于cv2編碼
4 encode_image = cv2.imencode(".jpg", img_array)[1] #用cv2壓縮/編碼,轉(zhuǎn)為一維數(shù)組
5 byte_data = encode_image.tobytes() #轉(zhuǎn)換為二進(jìn)制
6 base64_str = base64.b64encode(byte_data).decode("ascii") #轉(zhuǎn)換為base64
7 return base64_str
8
9def base64ToImg(base64_str):
10 # 傳入為RGB格式下的base64,傳出為RGB格式的numpy矩陣
11 byte_data = base64.b64decode(base64_str)#將base64轉(zhuǎn)換為二進(jìn)制
12 encode_image = np.asarray(bytearray(byte_data), dtype="uint8")# 二進(jìn)制轉(zhuǎn)換為一維數(shù)組
13 img_array = cv2.imdecode(encode_image, cv2.IMREAD_COLOR)# 用cv2解碼為三通道矩陣
14 img_array = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB)# BGR2RGB
15 return img_array
評(píng)論