1.try...exception中不要在exception中用if不会执行
2.python3 读取中文文档
import codecs
f = codecs.open('mix.label.scp', 'r+', encoding='utf-8')
3.csv文件按行写入并去除空行
import csv
with open('length_distribution.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['length', 'frequent'])
for i, (length, frequent) in enumerate(recounted):
writer.writerow([length, frequent])
4.统计list中元素出现频率并按key的大小进行排序
from collections import Counter
recounted = Counter(length_list)
recounted = sorted(recounted.items(), key= lambda recounted:recounted[0])
5.判断一个字符是英文还是中文还是数字
import string
if u'\u4e00' <= sentences[0][0] <= u'\u9fff':
print('Chinese')
if display_text_input[k] in string.ascii_letters or display_text_input[k].isdigit():
text_label[k] = 1
6.多线程
import concurrent.futures
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.map(get_val_result, sample_list)
7.文件复制
import shutil
shutil.copy(img_path,target_path )
8.统计numpy矩阵中单个元素的数量
np.sum( ndarray == 1 )
9.线程设置
设置:export OMP_NUM_THREADS=8
查看单个变量:echo $OMP_NUM_THREADS
查看全部变量:export
删除:unset OMP_NUM_THREADS
10.任务转后台生成日志
nohup ./train.sh>>trst.log 2>&1 &