赞
踩
文件包含:5000 + 用户头像;生成的 josn 用户数据包含:用户ID 头像 昵称
- import os
- import re
-
-
- def changename(orignname):
- index = 1
- picture = os.listdir(orignname)
- for filename in picture:
- # filename1 = filename.split(".")[0]
- # filename2=re.findall(r"\d+\.?\d*", filename1)[0]+".png"
- # srcpath = os.path.join(orignname,filename)
- # allpath = os.path.join(orignname,filename2)
- # os.rename(srcpath,allpath)
-
- # split("_",2)[1] “_”表示分隔符 ; 2表示分割次数 ; [1]表示选取第 i 个片段
- # filename1=filename.replace('用户头像','user_icon_')
- # filename1=filename.replace(' ','')
-
- filename1 = 'user_icon_{0:06d}.jpg'.format(index)
- # 设置旧文件名(就是路径+文件名)
- srcpath = os.path.join(orignname, filename)
- # 设置新文件名
- allpath = os.path.join(orignname, filename1)
- os.rename(srcpath, allpath)
- index += 1
-
-
- if __name__ == '__main__':
- orignname = r"./userIcon"
- changename(orignname)
- import os
- import json
-
- from openpyxl import load_workbook
- wb = load_workbook('name.xlsx')
- sheets = wb.worksheets # 获取当前所有的sheet
- print(sheets)
-
- # 获取第一张sheet
- sheet1 = sheets[0]
- # sheet1 = wb['Sheet'] # 也可以通过已知表名获取sheet
- print(sheet1)
-
- row_num = sheet1.max_row
- col_num = sheet1.max_column
- orignname = r"./userIcon"
- picture = os.listdir(orignname)
-
-
- jsontext = {'points': []}
- index = 1
- # with open("text.txt", "w") as file:
- for filename in picture:
- index += 1
- # file.writelines(json.dumps({'username': sheet1.cell(
- # index, 1).value, 'gender': 0, 'usericon': filename}))
- jsontext['points'].append(
- {'username': sheet1.cell(index, 1).value, 'id': '{0:06d}'.format(index), 'gender': 0, 'usericon': filename})
- # file.close()
- # print(jsontext)
- with open("text.json", "w",encoding='utf-8') as file:
- file.write(json.dumps(jsontext))
- file.close()
-
- # # 通过Cell对象读取
- # cell_11 = sheet1.cell(1,1).value
- # print(cell_11)
- # cell_11 = sheet1.cell(1,2).value
- # print(cell_11)
资源下载地址:https://download.csdn.net/download/nicepainkiller/87350953
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。