python常用的几个代码
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
https://pypi.tuna.tsinghua.edu.cn/simple # 清华源
http://mirrors.aliyun.com/pypi/simple/ # 阿里源
http://pypi.douban.com/simple/ #豆瓣
json的读写
info_path = '%s/%s' % (root_path, id)
with open(info_path) as json_file:
video_info = json.load(json_file)
json_file.close()
results = []
save_json = 'result.json'
with open(save_json, 'w') as f:
json.dump(results, f, indent=4)
f.close()
csv的读写
import csv
labelList = csv.reader(open(args.label, "rt", encoding="utf-8-sig"))
for row in labelList:
img_id = row[0]
with open(os.path.join(root_path, 'label/rec_pairs_v2.csv'), 'w', encoding='utf-8-sig') as ff:
headers = ['biz_no_1', 'minio_path_1']
f_scv = csv.DictWriter(ff, headers)
f_scv.writeheader()
f_scv.writerows(np.array(results))
ff.close()
panda读写csv
sheet = pd.read_csv(os.path.join(root_path, 'label/rec_pairs.csv'))
for i in sheet.values[:]:
path_1, path_2, label = i[0],i[1],i[2]
# 字典
dict = {'minio_path_1': nme, 'biz_no_1': st}
df = pd.DataFrame(dict)
# 保存 dataframe
df.to_csv('site.csv')
txt读写
f = open("test.txt", "r")
for line in f.readlines():
line = line.strip('')
f.close()
f=open(file=r'txt文件路径',mode='a')#w为创建新文件,x为创建新文件,同名就失败,a为添加到已有文件
f.write('xxx')
f.close()
base64
def get_base64(img_dir):
with open(img_dir, 'rb') as f:
base64_data = base64.b64encode(f.read())
base64_code = base64_data.decode()
f.close()
return base64_code