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
相关推荐
Eloudy10 小时前
LLM 公司提供的 token api 服务输出内容的特点
人工智能
DeepAgent10 小时前
AI Agent 工程实践(03):Memory 设计——Agent 到底应该记住什么?
人工智能
懂懂笔记11 小时前
老龄化加速下的中国答案:东软用AI思维重构“人生下半场”
人工智能·智慧养老
VIP_CQCRE11 小时前
用 Ace Data Cloud 快速接入 OpenAI Chat Completions:对话、流式、多轮和多模态一篇打通
python·ai·openai·api·教程
GitCode官方11 小时前
基于《人工智能 智能体互联》国标的 AIP 开源项目在 AtomGit 正式开源
人工智能·开源·atomgit
万象AI实验室11 小时前
GPT-5.6 正式上线,我们挖到了 3 个隐藏玩法!
人工智能
IpdataCloud11 小时前
AI生成的Avalon恶意框架怎么防?用IP离线库识别模块化C2通信
运维·人工智能·网络协议·tcp/ip·ip
FreeBuf_11 小时前
公开GitHub Issue可诱骗AI泄露私有数据,绕过防护仅需一词
人工智能·github·issue
kiros_wang11 小时前
ExploreYC 新手快速入门与实战指南
大数据·人工智能·物联网
阿里云大数据AI技术11 小时前
Elasticsearch 智能助手:Agent 让运维从经验驱动迈向智能协同
人工智能·elasticsearch·agent