5.5 KiB
5.5 KiB
In [18]:
import openpyxl
import json
from datetime import date
wb = openpyxl.load_workbook('data/沧州炼化人员.xlsx')
sheet = wb.active
# sheets = wb.sheetnames
person = {}
for n in range(2, sheet.max_row+1):
code = sheet.cell(n, 2).value
person.setdefault(code, {})
dict1 = {}
dict1['name'] = sheet.cell(n, 4).value
dict1['sex'] = sheet.cell(n, 5).value
dict1['birth'] = str(sheet.cell(n, 6).value).replace('/','-').split(' ')[0]
dict1['unit'] = sheet.cell(n, 3).value
dict1['phone'] = str(sheet.cell(n, 7).value)
person[code] = dict1
filename = 'data/沧州炼化人员.json'
with open(filename, 'w') as fl:
json.dump(person, fl, ensure_ascii=False)
print(len(person),'ok')1299 ok
In [9]:
import openpyxl
import json
from datetime import date
wb = openpyxl.load_workbook('data/沧州炼化人员.xlsx')
sheet = wb.active
# sheets = wb.sheetnames
person = {}
list1 = []
list2 = []
for n in range(2, sheet.max_row+1):
code = sheet.cell(n, 2).value
if code not in list1:
list1.append(code)
else:
list2.append(code)
list3 = []
for n in range(2, sheet.max_row+1):
code = sheet.cell(n, 2).value
if code in list2:
list4 = [code,sheet.cell(n, 4).value,sheet.cell(n, 3).value]
list3.append(list4)
print(list3)
filename = 'data/沧州炼化编号重复人员.xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
for row in list3:
sheet.append(row)
wb.save(filename)[['1553904', '孙建国', '物资采购中心'], ['1553904', '张巍', '应急救援中心']]
In [19]:
import json
filename = 'data/沧州炼化人员.json'
with open(filename,'r') as fl:
dict1 = json.load(fl)
list1 = []
for k, v in dict1.items():
dict2 = {}
#if dict1['sex'] =='男':
# sex = 1
dict2 = {'id':k,'name':v['name'],'gender':v['sex'],'birth':v['birth'],'unit':v['unit']}
list1.append(dict2)
json_data = json.dumps(list1,ensure_ascii=False, indent=4)
# 将 json 数据写入文件
with open("data/card_沧州炼化人员.json", "w",encoding = 'utf-8') as file:
file.write(json_data)
print(len(list1),'ok')1297 ok
In [ ]: