|
@@ -137,9 +137,9 @@ class DataManager:
|
|
|
@staticmethod
|
|
@staticmethod
|
|
|
def load_data_from_csv_file(file_path: str) -> List[Dict[str, Any]]:
|
|
def load_data_from_csv_file(file_path: str) -> List[Dict[str, Any]]:
|
|
|
"""
|
|
"""
|
|
|
- 从CSV文件中加载数据
|
|
|
|
|
- :param file_path: json文件绝对路径
|
|
|
|
|
- :return:
|
|
|
|
|
|
|
+ 从CSV文件中加载数据,自动转换数字类型
|
|
|
|
|
+ :param file_path: CSV文件绝对路径
|
|
|
|
|
+ :return: 处理后的数据列表
|
|
|
"""
|
|
"""
|
|
|
|
|
|
|
|
if not os.path.exists(file_path):
|
|
if not os.path.exists(file_path):
|
|
@@ -150,11 +150,29 @@ class DataManager:
|
|
|
json_list = [row for row in reader]
|
|
json_list = [row for row in reader]
|
|
|
|
|
|
|
|
if not isinstance(json_list, list):
|
|
if not isinstance(json_list, list):
|
|
|
- raise ValueError("JSON文件内容必须是数组格式")
|
|
|
|
|
|
|
+ raise ValueError("CSV文件内容必须是数组格式")
|
|
|
|
|
|
|
|
if not json_list:
|
|
if not json_list:
|
|
|
raise ValueError("数据文件为空")
|
|
raise ValueError("数据文件为空")
|
|
|
|
|
|
|
|
|
|
+ # 定义需要转换为数字的字段
|
|
|
|
|
+ numeric_fields = ['txAmount', 'txBalance']
|
|
|
|
|
+
|
|
|
|
|
+ # 对每条记录进行数字类型转换
|
|
|
|
|
+ for record in json_list:
|
|
|
|
|
+ for field in numeric_fields:
|
|
|
|
|
+ if field in record and record[field] is not None and record[field] != '':
|
|
|
|
|
+ try:
|
|
|
|
|
+ # 尝试转换为float,如果是整数则转换为int
|
|
|
|
|
+ value = float(record[field])
|
|
|
|
|
+ if value == int(value):
|
|
|
|
|
+ record[field] = int(value)
|
|
|
|
|
+ else:
|
|
|
|
|
+ record[field] = value
|
|
|
|
|
+ except (ValueError, TypeError):
|
|
|
|
|
+ # 如果转换失败,保持原字符串格式
|
|
|
|
|
+ pass
|
|
|
|
|
+
|
|
|
return json_list
|
|
return json_list
|
|
|
|
|
|
|
|
def write_json_to_csv(json_data, csv_file_path, field_order=None) -> bool:
|
|
def write_json_to_csv(json_data, csv_file_path, field_order=None) -> bool:
|