python代码实现读取excel文件提取文件中的usernameinstitutiondanwei列数据将其转化为json文件数据以字符串的格式传输无数据赋值为空字符串utf-8编码
以下是一个示例代码,使用Python的pandas库来读取Excel文件并将数据转换为JSON格式,并将其保存为UTF-8编码的文件。
import pandas as pd
import json
# 读取Excel文件
df = pd.read_excel('input.xlsx')
# 提取所需列数据
username = df['username'].fillna('').tolist()
institution = df['institution'].fillna('').tolist()
danwei = df['danwei'].fillna('').tolist()
# 构建JSON数据
data = []
for i in range(len(df)):
record = {
'username': username[i],
'institution': institution[i],
'danwei': danwei[i]
}
data.append(record)
# 保存为JSON文件
with open('output.json', 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False)
请将代码中的input.xlsx替换为你的Excel文件路径,并将生成的JSON文件保存在output.json中
原文地址: https://www.cveoy.top/t/topic/iUQu 著作权归作者所有。请勿转载和采集!