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
相关推荐
承渊政道几秒前
Python IDLE鸿蒙PC适配全记录:从Tkinter桌面程序到ArkUI原生开发闭环
开发语言·python·harmonyos·鸿蒙系统·桌面程序
七灵微8 分钟前
【AI】代码实战- 基于时间序列的轴承检测1
人工智能·机器学习·ai·pandas·matplotlib
OFIRM碳基硅基15 分钟前
西游金蝉劫 IP · 之 《金蝉子前传渡缘劫》电影 20 亿票房触发 · 项目专项扶持协议 总览 拉格朗日光影动画-西游金蝉劫项目组
大数据·人工智能·西游金蝉劫·蝎子精·蝎子精女妖王·金蝉子前传渡缘劫
RSABLOCKCHAIN16 分钟前
读懂AI-Agent项目Career-ops项目的底层原理
ai
人工干智能21 分钟前
爱因斯坦求和约定(Einstein Summation Convention)及einsum() 函数
python
YMWM_22 分钟前
查看realsense相机序列号脚本
开发语言·python·数码相机
银空飞羽24 分钟前
职型求职工具实测:把简历分析、岗位匹配和定向优化串成一条求职链路
人工智能·经验分享·面试·职场和发展·跳槽·求职招聘·创业创新
ChaITSimpleLove26 分钟前
AI时代 “本体” 的 “标准化” 定义
人工智能·dsl·rdf·ontology·ai本体·json-ld
FL162386312931 分钟前
基于YOLOv8的智慧校园人脸识别和公路汽车检测项目源码+训练好的模型
人工智能
进化矩阵34 分钟前
别把指标当目的:当数字开始反噬你
大数据·人工智能·职场和发展·创业创新