找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 1652|回复: 3

[求助] 求助:文本文件处理问题

2

主题

8

帖子

8

积分

贫民

积分
8
ee326 发表于 2019-8-29 11:20:22 | 显示全部楼层 |阅读模式
工作上遇到问题,需要将配置文件做变更,因为数据有数千笔,无法手动修改,希望各位协助。
原本设定
dot1q pvc 2 profile pppoe encapsulation multi
bind interface m210.201.21 ericsson
bind authentication chap context ericsson maximum 8
更改后设定
dot1q pvc 2 profile pppoe encapsulation multi
bind subscriber erison-1-2-0-2@ericsson
bind authentication chap context ericsson maximum 8
subscriber erison-1-2-0- 最后** 2 dot1q pvc 2 相同
范例文件如下

dot1q pvc 2 profile pppoe encapsulation multi
  bind interface m210.201.21 ericsson
   bind authentication chap context ericsson maximum 8
dot1q pvc 3 profile pppoe encapsulation multi
  bind interface m203.79.236 ericsson
   bind authentication chap context ericsson maximum 8
dot1q pvc 5 profile pppoe encapsulation multi
  bind interface m210.201.13 ericsson
   bind authentication chap context ericsson maximum 8
dot1q pvc 14 profile pppoe encapsulation multi
  bind interface m210.201.21 ericsson
   bind authentication chap context ericsson maximum 8
dot1q pvc 3996 profile pppoe encapsulation multi
  bind interface m203.79.237 ericsson
   bind authentication chap context ericsson maximum 8
dot1q pvc 3997 profile pppoe encapsulation multi
  bind interface m203.79.237 ericsson
   bind authentication chap context ericsson maximum 8


回复

使用道具 举报

0

主题

2

帖子

2

积分

贫民

积分
2
hujinqun 发表于 2019-8-30 10:07:18 | 显示全部楼层
    with open(filepath, 'r', encoding='utf-8') as rf:
        lines = rf.readlines()
        # print(lines)

    with open(filepath, 'w', encoding='utf-8') as wf:
        for index, line in enumerate(lines):
            if line.startswith('dot1q'):
                data = re.search(r'(?<=pvc\s)\d+', line).group()
                lines[index + 1] = 'bind subscriber erison-1-2-0-%s@ericsson\n'%data
        wf.writelines(lines)
回复 支持 反对

使用道具 举报

2

主题

8

帖子

8

积分

贫民

积分
8
ee326  楼主| 发表于 2019-8-30 17:23:05 | 显示全部楼层
Thank you very much.
回复 支持 反对

使用道具 举报

2

主题

8

帖子

8

积分

贫民

积分
8
ee326  楼主| 发表于 2019-9-2 10:07:47 | 显示全部楼层
根据提供的code修改
读取 example.txt,建立新的文件名 example1.txt,不会覆写源文件

import re

with open('example.txt', 'r', encoding='utf-8') as rf:
    lines = rf.readlines()
    # print(lines)

with open('example1.txt', 'w', encoding='utf-8') as wf:
    for index, line in enumerate(lines):
        if line.startswith('dot1q'):
            data = re.search(r'(?<=pvc\s)\d+', line).group()
            lines[index + 1] = 'bind subscriber erison-1-2-0-%s@ericsson\n' % data
    wf.writelines(lines)
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表