11import pandas as pd
22import io
33import os
4+ import re
45from openpyxl import Workbook
56from openpyxl .styles import Alignment , PatternFill
67from openpyxl .utils import get_column_letter
@@ -39,10 +40,21 @@ def worship():
3940
4041class CamelCaseUtil :
4142 """
42- 下划线形式(snake_case)转换为小驼峰形式(camelCase)工具方法
43+ 小驼峰形式(camelCase)与下划线形式(snake_case)互相转换工具方法
4344 """
4445 @classmethod
45- def __to_camel_case (cls , snake_str ):
46+ def camel_to_snake (cls , camel_str ):
47+ """
48+ 小驼峰形式字符串(camelCase)转换为下划线形式字符串(snake_case)
49+ :param camel_str: 小驼峰形式字符串
50+ :return: 下划线形式字符串
51+ """
52+ # 在大写字母前添加一个下划线,然后将整个字符串转为小写
53+ words = re .sub ('(.)([A-Z][a-z]+)' , r'\1_\2' , camel_str )
54+ return re .sub ('([a-z0-9])([A-Z])' , r'\1_\2' , words ).lower ()
55+
56+ @classmethod
57+ def snake_to_camel (cls , snake_str ):
4658 """
4759 下划线形式字符串(snake_case)转换为小驼峰形式字符串(camelCase)
4860 :param snake_str: 下划线形式字符串
@@ -64,7 +76,7 @@ def transform_result(cls, result):
6476 return result
6577 # 如果是字典,直接转换键
6678 elif isinstance (result , dict ):
67- return {cls .__to_camel_case (k ): v for k , v in result .items ()}
79+ return {cls .snake_to_camel (k ): v for k , v in result .items ()}
6880 # 如果是一组字典或其他类型的列表,遍历列表进行转换
6981 elif isinstance (result , list ):
7082 return [cls .transform_result (row ) if isinstance (row , (dict , Row )) else (cls .transform_result ({c .name : getattr (row , c .name ) for c in row .__table__ .columns }) if row else row ) for row in result ]
0 commit comments