python下修改配置文件参数

功能

由于刚好有需求要更改配置文件参数,想到python简单易用的特点,所以边学边做了这个程序帮助修改参数。该程序功能为读取一个二进制文本其中的参数按照指定格式修改。

参考教程

文件读写教程.
正则表达式教程.
python的很多基础都是在廖雪峰老师这学的,写的很详细不错,所以后面的分析就不详细说了。


源码

#!/usr/bin/python3
# coding: utf-8
speed = int(input("Please enter the adjustment speed: "))

#修改test.config文件
with open('test.config', 'r', encoding='utf-8') as o_config :
    lines = o_config.readlines()         # readlines返回每行字符串组成的list
    flen=len(lines)
    for i in range(flen):
        if lines[i].startswith("SIMULATION-TIME") :
            config_value = lines[i].split(' ')
            value = int( config_value[1][0:-2:1] )  # 用切片删去S和回车转成值         
            value = int(3600 / speed)
            lines[i] = config_value[0] + ' ' + str(value) + 'S\n'
            break
        else :
            continue
with open('test.config','w', encoding='utf-8') as new_config :   
    new_config.writelines(lines)

#修改test.nodes文件
with open('test.nodes', 'r', encoding='utf-8') as o_config :
    lines = o_config.readlines()         # readlines返回每行字符串组成的list
    flen=len(lines)
    for i in range(flen):
        if lines[i] == '\n' :
            break
        node_value = lines[i].split(' ', 2)
        if node_value[1] == '0' :
            continue
        else :
            value = int( node_value[1][0:-1:1] )
            value = int( value / speed )
            lines[i] = node_value[0] + ' ' + str(value) + 'S ' + node_value[2] 
with open('test.nodes','w', encoding='utf-8') as new_nodes :   
    new_nodes.writelines(lines)

print("Modified to complete")

代码分析

  1. python修改文本暂时没找到直接更改办法,上述代码都是先读取文本复制到内存中的列表,修改后再将列表写入文件。
  2. 修改test.config文件代码,使用readlines返回每行字符串组成的list。当然也可以用readline和while读取每一行来进行操作。
    使用startswith对每行行首判断寻找关键字符串类型,之后使用split空格分开为字符串列表后用切片提取出值对应的字符串。
    例如下图中config文件会读取到lines[8]这行有待寻找关键字,用split生成list[‘SIMULATION-TIME’, ‘3600S\n’],
    进行切片操作[1:-2:1]得到字符串’3600’ (回车\n也需要切片去除),这样转化成int型处理完再转为字符串填充回原来列表中即可。
    config文件
  3. 之后类似的对test.nodes进行操作test.nodes文件
    此处,由于其配置文件规则简单由空格区分,所以处理起来只需要split和列表切片,若是复杂的字符串数据还是得用上面教程中的正则表达式来处理。
    此外,如果需要更改参数精度更好的案例这里也提供一份
#!/usr/bin/python3
# coding: utf-8

#修改 1.nodes文件
speed = 100
with open('1.nodes', 'r', encoding='utf-8') as o_config :
    lines = o_config.readlines()         # readlines返回每行字符串组成的list
    flen=len(lines)
    for i in range(flen):
        if lines[i] == '\n' :
            break
        node_value = lines[i].split(' ', 5)

        valuex = round( float( node_value[2][1:-1:1]) / speed,14 )
        valuey = round( float( node_value[3][1:-1:1]) / speed,14 )
        valuez = round( float( node_value[4][1:-1:1]) / speed,14 )
        # lines[i] = node_value[0] + ' ' + node_value[1] + ' (' + \
        #     str(valuex) + ', ' + str(valuey) + ', ' + str(valuez) + ') ' + node_value[5]
        lines[i] = node_value[0] + ' ' + node_value[1] + ' (' + \
            '%.014f'%valuex + ', ' + '%.014f'%valuey + ', ' + '%.014f'%valuez + ') ' + node_value[5]
with open('1.nodes','w', encoding='utf-8') as new_nodes :   
    new_nodes.writelines(lines)

print("Modified to complete")

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值