str_utils.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2023/7/21 14:45
  3. # @Author : XuJiakai
  4. # @File : str_utils
  5. # @Software: PyCharm
  6. import json
  7. import re
  8. def json_str_2_list(json_str, key: str):
  9. if json_str is None:
  10. return []
  11. try:
  12. if type(json_str) == dict or type(json_str) == list:
  13. j = json_str
  14. else:
  15. j = json.loads(json_str)
  16. result = []
  17. for i in j:
  18. result.append(i[key])
  19. pass
  20. return result
  21. pass
  22. except Exception:
  23. return []
  24. pass
  25. pass
  26. def pascal_case_to_snake_case(camel_case: str):
  27. """
  28. 驼峰转下划线
  29. :param camel_case:
  30. :return:
  31. """
  32. snake_case = re.sub(r"(?P<key>[A-Z])", r"_\g<key>", camel_case)
  33. return snake_case.lower().strip('_')
  34. pass
  35. def snake_case_to_pascal_case(snake_case: str):
  36. """
  37. 下划线转驼峰
  38. :param snake_case:
  39. :return:
  40. """
  41. words = snake_case.split('_')
  42. return ''.join(word.title() for word in words)
  43. pass
  44. if __name__ == '__main__':
  45. res = pascal_case_to_snake_case("CompanyCourtOpenAnnouncement")
  46. print(pascal_case_to_snake_case("CompanyCourtOpenAnnouncement"))
  47. print(snake_case_to_pascal_case(res))
  48. pass