亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 re 模塊獲取 txt 文件中的特定重復部分

使用 re 模塊獲取 txt 文件中的特定重復部分

RISEBY 2021-12-08 14:40:06
如何使用 re 模塊復制文本文件中以特定單詞開頭和結尾的重復段落,并在列表索引中插入每個段落段落示例RemoteConfig cleanup.txt --prefix  /tftpboot/sites --status  /tftpboot/190315_140116/cleanup/stat_KSHA500 --conf /export/home-V cisco -N -S -o -P telnet,ssh2RemoteConfig cleanup.txt --prefix  /tftpboot/sites --status  /tftpboot/190315_140200/cleanup/stat_KSHA11 --conf /export/home-V cisco -N -S -o -P telnet,ssh2RemoteConfig cleanup.txt --prefix  /tftpboot/sites --status  /tftpboot/190315_1500/cleanup/stat_KSHA211 --conf /export/home-V cisco -N -S -o -P telnet,ssh2
查看完整描述

1 回答

?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

除了語言障礙,根據您的標準試試這個


>>> import re


>>> string = '''RemoteConfig cleanup.txt --prefix  /tftpboot/sites --status  /tftpboot/190315_140116/cleanup/stat_KSHA500 --conf /export/home-V cisco -N -S -o -P telnet,ssh2

RemoteConfig cleanup.txt --prefix  /tftpboot/sites --status  /tftpboot/190315_140200/cleanup/stat_KSHA11 --conf /export/home-V cisco -N -S -o -P telnet,ssh2

RemoteConfig cleanup.txt --prefix  /tftpboot/sites --status  /tftpboot/190315_1500/cleanup/stat_KSHA211 --conf /export/home-V cisco -N -S -o -P telnet,ssh2'''



>>> configs = re.findall('(?i)RemoteConfig[\S\s]*?ssh2(?=\s|$)', string)



>>> for config in configs:

        print(config)


#Output

RemoteConfig cleanup.txt --prefix  /tftpboot/sites --status  /tftpboot/190315_140116/cleanup/stat_KSHA500 --conf /export/home-V cisco -N -S -o -P telnet,ssh2

RemoteConfig cleanup.txt --prefix  /tftpboot/sites --status  /tftpboot/190315_140200/cleanup/stat_KSHA11 --conf /export/home-V cisco -N -S -o -P telnet,ssh2

RemoteConfig cleanup.txt --prefix  /tftpboot/sites --status  /tftpboot/190315_1500/cleanup/stat_KSHA211 --conf /export/home-V cisco -N -S -o -P telnet,ssh2





>>> configs


#Output

['RemoteConfig cleanup.txt --prefix  /tftpboot/sites --status  /tftpboot/190315_140116/cleanup/stat_KSHA500 --conf /export/home-V cisco -N -S -o -P telnet,ssh2', 'RemoteConfig cleanup.txt --prefix  /tftpboot/sites --status  /tftpboot/190315_140200/cleanup/stat_KSHA11 --conf /export/home-V cisco -N -S -o -P telnet,ssh2', 'RemoteConfig cleanup.txt --prefix  /tftpboot/sites --status  /tftpboot/190315_1500/cleanup/stat_KSHA211 --conf /export/home-V cisco -N -S -o -P telnet,ssh2']



查看完整回答
反對 回復 2021-12-08
  • 1 回答
  • 0 關注
  • 272 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號