1. 新建文件夹
if not os.path.exists(feature_dir):
os.makedirs(feature_dir)
2. 后台运行并保存log
nohup python -u test.py > test.log 2>&1 &
tail -f test.log
cat test.log
3. 文件读取
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)
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()
x = {..}
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)
with open(r"./x.json",'r') as f:
x = json.loads(f.readlines()[0])
with open(r"./x.json",'r') as f:
x = json.load(f)
x = [x,]
np.save("./././x.npy",x)
x = np.load(r"./././x.npy")
data = pd.read_excel(r'xxxx.xlsx','Sheet1')
result = {x:1,y:2,..}
df = pd.DataFrame(list(result.items()), columns=['key','value'])
df.to_csv(r"./result.csv", index=False,header=True)
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(y)
Counter('1232')['2']
6. timestamp 转换标准时间
import time
def timestamp_datetime(value):
format = '%Y-%m-%d %H:%M:%S'
value = time.localtime(value)
dt = time.strftime(format, value)
return dt
def datetime_timestamp(dt):
time.strptime(dt, '%Y-%m-%d %H:%M:%S')
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. 排序
listX = [[1,4],[2,5],[3,3]]
sorted(listX, key=lambda x : x[1])
list1 = [1, 2, 3, 4, 15, 6]
list2 = ['a', 'b', 'c', 'd', 'e', 'f']
c = list(zip(list1,list2))
c.sort(reverse=True)
list1[:],list2[:] = zip(*c)
print(list1,list2)
8. 文件路径获取
path1 = os.getcwd()
path2 = os.path.dirname(os.path.realpath(__file__))
9. 同一行刷新打印
print("\r",object,end="",flush=True)
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):
img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
encode_image = cv2.imencode(".jpg", img_array)[1]
byte_data = encode_image.tobytes()
base64_str = base64.b64encode(byte_data).decode("ascii")
return base64_str
def base64ToImg(base64_str):
byte_data = base64.b64decode(base64_str)
encode_image = np.asarray(bytearray(byte_data), dtype="uint8")
img_array = cv2.imdecode(encode_image, cv2.IMREAD_COLOR)
img_array = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB)
return img_array