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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值