利用Python处理数据,并存储为yanl格式

今天整理了一份代码,是将数据分开整理保存为yaml格式。比较简单,但也是我接触企业的算是第一个与代码有关的了。(以下数据是伪造数据,是王者荣耀的英雄属性值)

# cat data.txt 
name=孙悟空;maxLife=6585;lifeGrowth=235.1;initialLife=3293;maxMana=1760;manaGrowth=95;initialMana=430;maxPhysicalAttack=349;PhysicalAttackGrowth=13;initialPhysicalAttack=167;maxPhysicalDefense=385;PhysicalDefenseGrowth=20.79;initialPhysicalDefense=94;maxRestore5s=87;Restore5sGrowth=2.643;initialRestore5s=50;maxRestoreBlue5s=37;RestoreBlue5sGrowth=1.571;initialRestoreBlue5s=15;maxAttackSpeed=14%;mainPosition=战士;secondaryPosition=刺客-name=李白;maxLife=5483;lifeGrowth=179.6;initialLife=2968;maxMana=1808;manaGrowth=97;initialMana=450;maxPhysicalAttack=330;PhysicalAttackGrowth=11.5;initialPhysicalAttack=169;maxPhysicalDefense=358;PhysicalDefenseGrowth=18.57;initialPhysicalDefense=98;maxRestore5s=80;Restore5sGrowth=2.214;initialRestore5s=49;maxRestoreBlue5s=38;RestoreBlue5sGrowth=1.571;initialRestoreBlue5s=16;maxAttackSpeed=42%;mainPosition=刺客;secondaryPosition=战士-name=貂蝉;maxLife=5611;lifeGrowth=185.1;initialLife=3019;maxMana=1960;manaGrowth=105;initialMana=490;maxPhysicalAttack=287;PhysicalAttackGrowth=8.571;initialPhysicalAttack=167;maxPhysicalDefense=330;PhysicalDefenseGrowth=17.36;initialPhysicalDefense=87;maxRestore5s=71;Restore5sGrowth=2;initialRestore5s=43;maxRestoreBlue5s=41;RestoreBlue5sGrowth=1.714;initialRestoreBlue5s=17;maxAttackSpeed=14%;mainPosition=法师;secondaryPosition=刺客-name=雅典娜;maxLife=6264;lifeGrowth=243;initialLife=2862;maxMana=1732;manaGrowth=93;initialMana=430;maxPhysicalAttack=327;PhysicalAttackGrowth=11.79;initialPhysicalAttack=162;maxPhysicalDefense=418;PhysicalDefenseGrowth=22.29;initialPhysicalDefense=106;maxRestore5s=83;Restore5sGrowth=2.786;initialRestore5s=44;maxRestoreBlue5s=36;RestoreBlue5sGrowth=1.5;initialRestoreBlue5s=15;maxAttackSpeed=28%;mainPosition=战士;secondaryPosition=坦克-name=百里守约;maxLife=5611;lifeGrowth=185.1;initialLife=3019;maxMana=1784;manaGrowth=96;initialMana=440;maxPhysicalAttack=410;PhysicalAttackGrowth=15.86;initialPhysicalAttack=188;maxPhysicalDefense=329;PhysicalDefenseGrowth=16.79;initialPhysicalDefense=94;maxRestore5s=68;Restore5sGrowth=2.071;initialRestore5s=39;maxRestoreBlue5s=38;RestoreBlue5sGrowth=1.571;initialRestoreBlue5s=16;maxAttackSpeed=28%;mainPosition=射手;secondaryPosition=刺客
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __Author__ = Anasta
# Date = 18-7-6

"""
将数据整理为单个yaml文件,
文件以name的值命名,
即hero_nameValue.yaml
"""

import io

with io.open('./data.txt', 'r', encoding='utf-8') as f:
    dStr = f.read()

# 先将数据用‘-’分组
data_src = dStr.split('-')

for x,y in enumerate(data_src, 1):
    # 初始化文件名为序号
    filename = str(x) + '.yaml'
    data = y.split(';')
    # 定义一个临时列表
    _tmp_data = []
    for i in data:
        k, v = i.split('=')
        # 将文件名改为文件里的关键字
        if k == 'name':
            filename = 'hero_' + v + '.yaml'
        # 将即将要存到文件里的内容格式先一组一组的存到列表里
        _tmp_data.append('{}: {}\n'.format(k, v))
    # 为防止重复执行该脚本,利用列表一次性将一组数据通过w写入,以防追加文件
    with io.open(filename, 'w', encoding='utf-8') as f:
        f.writelines(_tmp_data)

print('Done!')

tips:

使用with open 读取或编写文件时,会自动帮我们调用close()函数关闭资源

在代码中用列表将一组数据单独存起来从而可以用“w”存到文件里,以防用“a”一个一个追加后又重复执行,这样就会永无止境的往文件里写数据。

在给文件名赋值之前要先初始化filename,给它一个值,以防再下面的数据没有name,无法取到值。

我的编译环境为python3.6,有时有的个别公司项目为2.7,所以需测试。发现若为python2.7时,在往_tmp_data列表里添加元素的时候会发生错误。

# python Conversion_WangZhe_yaml.py

Traceback (most recent call last):
  File "Conversion_WangZhe_yaml.py", line 32, in 
    f.writelines(_tmp_list)
TypeError: must be unicode, not str

说需要为unicode字符编码,所以再添加些东西。要在前面添加的元素前面加一个“u”,即_tmp_data.append(u"{}: {}\n".format(k, v))  

你可能感兴趣的:(Python)