BP_AIChar_110020100_CPNSoldier ---- 2113 MB GameAIBehaviorCPN110020100_CPNSoldier ---- 2113 MB E ---- GameMapsC02_StormFortressQuestSequenceM_Light_shinny E ---- GameAvatarBa
假设已经读取了文件内容,存储在变量file_content中:
import re from django.db import transaction from myapp.models import PackageAssetInfo
def parse_size(size_str): """将文件大小字符串转换为字节数""" units = {'KB': 1024, 'MB': 10242, 'GB': 10243} size, unit = re.match(r'^([\d.]+)\s*(\w+)$', size_str).groups() return int(float(size) * units[unit])
def save_package_asset_info(file_content): """将文件内容入库""" with transaction.atomic(): # 先清空表中数据 PackageAssetInfo.objects.all().delete() # 逐行解析并入库 for line in file_content.split('\n'): if not line.strip(): # 跳过空行 continue depth = line.count(' ') // 4 # 根据缩进计算层级 is_leaf = not line.endswith('/') # 判断是否是叶节点 parts = line.split() size = parse_size(parts[1]) if len(parts) > 1 else None PackageAssetInfo.objects.create( id_test=0, type=parts[0] if is_leaf else 'dir', name=parts[-1], size=size, id_row=0, parent_id=None if depth == 0 else None, # 根节点没有父节点 depth=depth, leaf=is_leaf, )
save_package_asset_info(file_content
原文地址: https://www.cveoy.top/t/topic/cRKt 著作权归作者所有。请勿转载和采集!