python 处理json、excel、然后将内容转化为DSL语句,适用于数据处理(实用版)

整个Python 的项目结构

Python

-config

-dev.py

-data

-data.json

-dsl.json

-dsl.txt

-dsl模版.json

-example.xls

-将execel里面的内容转成DSL语句.xlsx

-doc

design.md

-src

-models

-_init_py

-main.py

-utils

-init .py

-json_load_utils.py

-README.md

-requirements.txt

-setup.py

内容:存放excel处理的工具类的内容,包括读取excel,读取python ,转DSL

json_load_utils.py
python 复制代码
import json
import openpyxl
from openpyxl.reader.excel import load_workbook
from string import Template


# 可通过类名直接访问工具类
def get_json_file_to_data():  # 读取json 文件,然后将json数据返回
    with open('D:/tools/tools/python/data/data.json', 'r', encoding='utf-8') as file:  # 读取json文件转化为数据
        data = json.load(file)
        return data;


def get_json_file_from_dsl():  # 读取json 文件,然后将json 数据转换为Excel
    with open('D:/tools/tools/python/data/dsl.json', 'r', encoding='utf-8') as file:
        data2 = json.load(file)
        dataValue = data2.get("hits").get("hits")  # 解析数组里面的内容
        wb = openpyxl.Workbook()
        ws = wb.active
        ws['A1'] = 'username'
        ws['B1'] = 'nickname'
        ws['C1'] = 'age'
        ws['D1'] = 'money'
        for Object in dataValue:
            username = Object.get('_source').get('username')
            nickname = Object.get('_source').get('nickname')
            age = Object.get('_source').get('age')
            money = Object.get('_source').get('money')
            rows = [username, nickname, age, money]
            ws.append(rows)
            wb.save('D:/tools/tools/python/data/exapme.xls')


def get_json_file_to_dsl():  # 读取json 文件,然后将json 数据转换为Excel
    wb = load_workbook(r'D:/tools/tools/python/data/将excel里面的内容转成DSL语句.xlsx')
    sheet = wb.active  # 默认读取第一个工作簿
    data = []
    for row_idx in sheet.iter_rows(min_row=2, values_only=True):  # 从第2行开始读取数据
        # username = row.username
        # nickname = row.nickname
        # age = row.age
        # money = row.money)
        data.append(row_idx)
    return data
    # 遍历所有的文件,然后将数据写入到DSL语句中


with open('D:/tools/tools/python/data/dsl.txt', 'w',encoding='utf-8') as file:
    i = 0
    data2 = get_json_file_to_dsl()
    for row in data2:
        # 索引第一句
        username = row[0]
        nickname = row[1]
        age = row[2]
        money = row[3]
        index_name = "XXX/XXX"
        index_id = i
        s1 = Template('POST $index_name/$index_id \n')
        t1 = s1.substitute(index_name=index_name, index_id=index_id)
        s2 = Template(
            '{\n"username": "$username",\n "nickname": "$nickname",\n "age": "$age",\n "money": "$money"\n}')
        t2 = s2.substitute(username= username,nickname=nickname,age=age,money=money)
        # t1.safe_substitute(index_name, index_id)
        # t2.safe_substitute(username, nickname, age, money)
        file.write(t1)
        file.write(t2)
        i = i + 1
main.py
python 复制代码
# This is a sample Python script.
from utils.json_load_utils import get_json_file_to_data, get_json_file_to_dsl, get_json_file_from_dsl


# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
    data = get_json_file_to_data()
    print(data)
    get_json_file_from_dsl()
    get_json_file_to_dsl()


# See PyCharm help at https://www.jetbrains.com/help/pycharm/
data.json
python 复制代码
{

        "name":"1",
         "age":18
}
dsl.json
python 复制代码
{
  "took": 0,
  "time_out": false,
  "_shards": {
    "total": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "hits": [{
      "_index": "idx_20221124",
      "_type": "_doc",
      "_id": "20200001",
      "_score": 1.0,
      "_source": {
        "username": "test-name-1",
        "nickname": "我的昵称",
        "age": 20,
        "money": 1024.06
      }
    }]

  }
}
DSL.txt
python 复制代码
POST XXX/XXX/0 
{
"username": "test-name-1",
 "nickname": "我的昵称",
 "age": "20",
 "money": "1024.06"
}
dsl模板.json
python 复制代码
POST $indexName/$id
{
  "$key1": "$keyValue1",
  "$key2":"$keyValue2",
  "$key3": "keyValue3",
  "key4": "keyValue4"
}
exapme.xls 里面的内容
将excel里面的内容转成DSL语句.xlsx
相关推荐
丝斯20113 分钟前
AI学习笔记整理(66)——多模态大模型MOE-LLAVA
人工智能·笔记·学习
小鸡吃米…23 分钟前
机器学习中的代价函数
人工智能·python·机器学习
chatexcel1 小时前
元空AI+Clawdbot:7×24 AI办公智能体新形态详解(长期上下文/自动化任务/工具粘合)
运维·人工智能·自动化
Li emily1 小时前
如何通过外汇API平台快速实现实时数据接入?
开发语言·python·api·fastapi·美股
bylander1 小时前
【AI学习】TM Forum《Autonomous Networks Implementation Guide》快速理解
人工智能·学习·智能体·自动驾驶网络
m0_561359671 小时前
掌握Python魔法方法(Magic Methods)
jvm·数据库·python
Ulyanov2 小时前
顶层设计——单脉冲雷达仿真器的灵魂蓝图
python·算法·pyside·仿真系统·单脉冲
Techblog of HaoWANG2 小时前
目标检测与跟踪 (8)- 机器人视觉窄带线激光缝隙检测系统开发
人工智能·opencv·目标检测·机器人·视觉检测·控制
laplace01232 小时前
Claude Skills 笔记整理
人工智能·笔记·agent·rag·skills
2501_941418552 小时前
【计算机视觉】基于YOLO11-P6的保龄球检测与识别系统
人工智能·计算机视觉