python 写 yml文件

​​yaml 另一种标记语言

import yaml​

import os

root_path = os.path.abspath(os.path.dirname(__file__)).replace('\\', '/')

print 'root_path: ' + root_path

config_file = os.path.abspath(os.path.join(root_path, 'test.yml')).replace('\\', '/')

print 'config_file: ' + config_file

file_path = os.path.abspath(os.path.realpath(__file__)).replace('\\', '/')

print 'file_path: ' + file_path

app_name_1 = os.path.split(file_path)

app_name_2 = os.path.splitext(file_path)

print app_name_1, app_name_2

app_name = app_name_1[1].split('.')[0]

print app_name

def write_config_yaml(Node_number, branch_name):

    with open(config_file, 'a+') as f:

        default_Node_information = {}

        print default_Node_information

        default_Node_information['branch'] = []

        print default_Node_information

        default_Node_information['branch'].append(branch_name)

        default_Node_information['branch'].append(app_name)     

        dataMap = dict.fromkeys([Node_number])   

        dataMap[Node_number] = default_Node_information

        print dataMap

        yaml.dump(dataMap, f, default_flow_style=False)

        

        

if __name__ == '__main__':

    f = open(config_file, 'w+')

    f.close()

    write_config_yaml(1, 'branch_name_1')

    



输出

pydev debugger: starting (pid: 6568)

root_path: C:/Users/b56853/workspace/Demo2/src/memo

config_file: C:/Users/b56853/workspace/Demo2/src/memo/test.yml

file_path: C:/Users/b56853/workspace/Demo2/src/memo/write_yaml.py

('C:/Users/b56853/workspace/Demo2/src/memo', 'write_yaml.py') ('C:/Users/b56853/workspace/Demo2/src/memo/write_yaml', '.py')

write_yaml

{}

{'branch': []}

{1: {'branch': ['branch_name_1', 'write_yaml']}}

test.yml文件中的内容:

1:

  branch:

  - branch_name_1

  - write_yaml

你可能感兴趣的:(python)