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
相关推荐
码界奇点8 小时前
Python从0到100一站式学习路线图与实战指南
开发语言·python·学习·青少年编程·贴图
爬点儿啥8 小时前
[Ai Agent] 10 MCP基础:快速编写你自己的MCP服务器(Server)
人工智能·ai·langchain·agent·transport·mcp
张人玉9 小时前
百度 AI 图像识别 WinForms 应用代码分析笔记
人工智能·笔记·百度
测试人社区-小明9 小时前
智能弹性伸缩算法在测试环境中的实践与验证
人工智能·测试工具·算法·机器学习·金融·机器人·量子计算
Spring AI学习9 小时前
Spring AI深度解析(9/50):可观测性与监控体系实战
java·人工智能·spring
Laravel技术社区9 小时前
pytesseract 中英文 识别图片文字
python
罗西的思考10 小时前
【Agent】MemOS 源码笔记---(5)---记忆分类
人工智能·深度学习·算法
unique_perfect10 小时前
vue2与springboot实现deepseek打印机聊天
spring boot·websocket·ai·vue2·deepseek
dajun18112345610 小时前
反 AI 生成技术兴起:如何识别与过滤海量的 AI 伪造内容?
人工智能
生骨大头菜10 小时前
使用python实现相似图片搜索功能,并接入springcloud
开发语言·python·spring cloud·微服务