整个Python 的项目结构
Python
-config
-dev.py
-data
-data.json
-dsl.json
-dsl.txt
-dsl模版.json
-example.xls
-将execel里面的内容转成DSL语句.xlsx
-doc
-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 里面的内容
