python代码实现读取excel文件提取文件中的usernameinstitutiondanwei列数据将其转化为json文件数据以字符串的格式传输无数据赋值为空字符串
以下是一种实现方法:
import pandas as pd
import json
# 读取Excel文件
df = pd.read_excel('data.xlsx')
# 提取指定列数据
username = df['username'].fillna('').tolist()
institution = df['institution'].fillna('').tolist()
danwei = df['danwei'].fillna('').tolist()
# 构建字典
data = {
'username': username,
'institution': institution,
'danwei': danwei
}
# 将字典转换为JSON字符串
json_str = json.dumps(data)
# 将JSON字符串保存到文件
with open('data.json', 'w') as f:
f.write(json_str)
注意,上述代码假设Excel文件名为data.xlsx,并且文件中的表格标题行为username、institution和danwei。如果实际情况不同,请根据实际情况修改代码中的相应部分
原文地址: https://www.cveoy.top/t/topic/iUQo 著作权归作者所有。请勿转载和采集!