python scan-input-for-ph.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python scan-input-for-ph.py相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python
# This should work with numbers that look like:
# (111) 111-1111
# (111)111-1111
# and much less well tested with 1111111111 :)
# From here it is a matter of adding recursive directory walking with
# os.walk, see: https://docs.python.org/3/library/os.html#os.walk
# and wrap `with open` context manager in a for loop which takes path
# yielded by os.walk and passes it to context manager where we can handle
# things like failure to open files, etc. Remainder should not require modification.

import re, sys
phre = re.compile(r'''(
    (\d{3}|\(\d{3}\))?
    (\s|-|\.)?
    (\d{3})
    (\s|-|\.)
    (\d{4})
    (\s*(ext|x|ext.)\s*(\d{2,5}))?
    )''', re.VERBOSE)

def read(file_obj):
    while True:
        data = file_obj.readline()
        if not data:
            break
        yield data

def process(line):
    s = phre.search(line)
    if s:
        return line[s.start():s.end()]
    return None

if __name__ == "__main__":
    if len(sys.argv[:]) < 2:
        sys.exit(1)
    with open(sys.argv[1], 'rb') as f:
        for line in read(f):
            result = process(line)
            if result:
                print(result)

以上是关于python scan-input-for-ph.py的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python