python读取本地文件,提取指定格式的内容

Updated on in 人生苦短,我用Python with 0 views and 0 comments

#-*- coding:utf-8 -*-
import sys
import re
def banner():
    print(' '*60)
    print('#'*60)
    print('#author laoyan')
    print('#This script is applied to Filtering IP information.')
    print('#'*60)
    print(' '*60)
def main(lyfile):
    try:
        f=open(lyfile,'r', encoding='UTF-8')
        ref=open('reloadips.txt','w', encoding='UTF-8')
        lyf=f.read()
        refind=re.findall(r'data-soundurl64=\".+?\"',lyf)
        print(refind)
        for i in range(len(refind)):
            a=refind[i].split('data-soundurl64="')
            b='https://static.missevan.com/'+a[1].split('"')[0]
            ref.write(b+'\n')
    except Exception as e:
        print('error occurs while reading file')
    finally:
        f.close()
        ref.close()
        print('Success!')
if __name__ == '__main__':
    if len(sys.argv) == 2:
        banner()
        lyfile=sys.argv[1]
        main(lyfile)
    else:
        print('useage: python reloadips.py filename')
        sys.exit(1)

标题:python读取本地文件,提取指定格式的内容
作者:MaidongAndYida
地址:https://cuijianzhe.github.io/articles/2019/08/20/1566269499265.html