LingxinMeng 6 miesięcy temu
rodzic
commit
0ce92c91c3
1 zmienionych plików z 48 dodań i 2 usunięć
  1. 48 2
      src/python2/pjibot_delivery/csv-pjibot_delivery.py

+ 48 - 2
src/python2/pjibot_delivery/csv-pjibot_delivery.py

@@ -4,6 +4,7 @@ import shutil
 import subprocess
 import time
 import oss2
+import json
 
 from resource import bagtocsv_robot
 
@@ -17,6 +18,47 @@ logging.basicConfig(filename=path1 + 'log/csv-pjibot_delivery.log', level=loggin
 
 key1 = 'pjibot_delivery/'
 sleep_time = 30  # 每多少秒扫描一次
+error_bag_json = "/mnt/disk001/dcl_data_process/src/python2/pjibot_delivery/csv-errorBag.json"
+
+
+def parse_json_to_string_array(file_path):
+    try:
+        # 打开并读取JSON文件(Python 2中不支持encoding参数,需要使用codecs模块或处理文件读取后的编码)
+        with open(file_path, 'r') as file:
+            # 读取文件内容
+            file_content = file.read()
+            # 解析JSON内容(Python 2中json.loads用于解析字符串)
+            data = json.loads(file_content.decode('utf-8'))  # 假设文件是UTF-8编码,这里需要手动解码
+
+        # 检查数据是否是一个列表,并且列表中的元素是否是字符串
+        if isinstance(data, list):
+            for item in data:
+                if not isinstance(item, basestring):  # Python 2中字符串类型包括str和unicode,用basestring检查
+                    raise ValueError("JSON数组中的元素不是字符串")
+            return data
+        else:
+            return []
+    except Exception as e:
+        return []
+
+
+def list_to_json_file(data, file_path):
+    """
+    将列表转换为JSON格式并写入指定的文件路径。
+    如果文件已存在,则覆盖它。
+
+    参数:
+    data (list): 要转换为JSON的列表。
+    file_path (str): 要写入JSON数据的文件路径。
+    """
+    # 将列表转换为JSON格式的字符串,并确保输出为UTF-8编码的字符串
+    json_data = json.dumps(data, ensure_ascii=False, indent=4)
+    json_data_utf8 = json_data.encode('utf-8')  # 编码为UTF-8
+
+    # 以写入模式打开文件,如果文件已存在则覆盖
+    with open(file_path, 'w') as file:
+        # 将UTF-8编码的JSON字符串写入文件
+        file.write(json_data_utf8)
 
 
 def parse_csv(data_bag, parse_prefix, local_parse_dir, local_delete_list):
@@ -93,7 +135,9 @@ def parse_csv(data_bag, parse_prefix, local_parse_dir, local_delete_list):
         local_delete_list.append(str(local_csv_dir) + '/report.pdf')
 
     except Exception as e2:
-        # 当出现异常时执行的代码
+        error_bag_list = parse_json_to_string_array(error_bag_json)
+        error_bag_list.append(parse_prefix)
+        list_to_json_file(error_bag_list, error_bag_json)
         logging.exception("生成csv报错: %s", e2)
 
 
@@ -137,7 +181,9 @@ if __name__ == '__main__':
                             pdf_done = True
                     if csv1_done and csv2_done and csv3_done and csv4_done and pdf_done:
                         continue
-
+                    error_bag_list = parse_json_to_string_array(error_bag_json)
+                    if parse_prefix_full in error_bag_list:
+                        continue
                     logging.info("开始生成场景还原csv: %s" % str(obj1.key))
                     local_merged_bag_path = path3 + merged_bag_object_key
                     local_merged_dir = '/'.join(local_merged_bag_path.split('/')[:-1])