Python:匹配另一个文件中的数字并返回参数

问题描述 投票:0回答:1

使用Python3 ..我正在努力从下面显示的文件“ NumberMapping.txt”中打印参数,该文件与我的CallData记录字段'A_ISDN'匹配。

'A_ISDN'字段是电话号码,我每次都会获得不同的号码,但是国家/地区前缀将相同。

CallData = 'A:QEC, B:SQ.O, Date:20181009, Duration:0, RecordClosing:480, A_ISDN:+9427159721922'
number = str(item).split('A_ISDN:')[1].split(',')[0].strip()if "A_ISDN:" in str(item) else ("")

number变量上方,我能够提取想要将起始数字与'NumberMapping.txt'中的数字匹配的+942759721922

cat NumberMapping.txt
+94:10001
+59:10002
+64:10003
+19:10004
+20:10005
+1:10006

expected output:
since above example has A_ISDN number started with +94, i need print/return corresponding parameter from 'NumberMapping.txt' file
10001

因此,如果CallData具有以+1开头的数字,我需要打印/返回相应的参数-10006

请帮助并提前致谢。

python python-3.x
1个回答
0
投票

我走得更远,因为我已经为先前的答案写了一些有关如何拆分.txt文件的代码。

# These variables would be loaded via `open`.
record_file = """A:QEC, B:SQ.O, Date:20181009, Duration:0, RecordClosing:480, A_ISDN:+9427159721922
A:BAC, B:SQ.O, Date:20281009, Duration:2, RecordClosing:523, A_ISDN:+127159721922"""
mapping_file = """+94:10001
+59:10002
+64:10003
+19:10004
+20:10005
+1:10006"""


def split_record(record):
    """ Splits the record file into a dictionary. """

    # Headers of the intended break-points in the record files.
    headers = ["A:", "B:", "Date:", "Duration:", "RecordClosing:", "A_ISDN:"]

    information = []

    # Split our record by lines.
    for line in record.split("\n"):

        # Split our record up so we only have the information per header.
        default_header = headers[0]
        for header in headers[1:]:
            line = line.replace(header, default_header)
        info = [i.strip() for i in line.split(default_header)][1:]

        # Compile our header+information together into OrderedDict's.
        compiled_information = {}
        for header, info in zip(headers, info):
            compiled_information[header] = info

        # Append to our overall information list.
        information.append(compiled_information)

    return information


def split_mapping(mapping):
    """ Splits the phone mapping of the format +ex:num into a dictionary. """

    info = {}
    for line in mapping.split("\n"):
        split = line.split(":")
        info[split[0]] = split[1]

    return info


def record_mapping(record, mapping, number_length):
    """ Correlates the records and the mappings together. """

    info = {}
    for record in records:
        phone = record["A_ISDN:"]
        extension = phone[::-1][number_length:][::-1]

        info[phone] = mappings[extension]

    return info


records = split_record(record_file)
mappings = split_mapping(mapping_file)
correlate = record_mapping(records, mappings, number_length=11)

print(correlate)

输出:

{'+9427159721922': '10001', '+127159721922': '10006'}

澄清

split_record()

第一个功能split_record()接受entire记录文件,并将其拆分为字典。假设您的记录文件是:

A:QEC, B:SQ.O, Date:20181009, Duration:0, RecordClosing:480, A_ISDN:+9427159721922
A:BAC, B:SQ.O, Date:20281009, Duration:2, RecordClosing:523, A_ISDN:+127159721922
A:HSF, B:SQ.O, Date:28481009, Duration:5, RecordClosing:942, A_ISDN:+5959274929424
A:BAC, B:SQ.O, Date:20141009, Duration:1, RecordClosing:131, A_ISDN:+2027159721922

它将输出:

[{'A:': 'QEC,',
  'A_ISDN:': '+9427159721922',
  'B:': 'SQ.O,',
  'Date:': '20181009,',
  'Duration:': '0,',
  'RecordClosing:': '480,'},
 {'A:': 'BAC,',
  'A_ISDN:': '+127159721922',
  'B:': 'SQ.O,',
  'Date:': '20281009,',
  'Duration:': '2,',
  'RecordClosing:': '523,'},
 {'A:': 'HSF,',
  'A_ISDN:': '+5959274929424',
  'B:': 'SQ.O,',
  'Date:': '28481009,',
  'Duration:': '5,',
  'RecordClosing:': '942,'},
 {'A:': 'BAC,',
  'A_ISDN:': '+2027159721922',
  'B:': 'SQ.O,',
  'Date:': '20141009,',
  'Duration:': '1,',
  'RecordClosing:': '131,'}]

split_mapping()

此功能只是拆分映射文件。如果映射文件是

+94:10001
+59:10002
+64:10003
+19:10004
+20:10005
+1:10006

返回

{'+1': '10006',
 '+19': '10004',
 '+20': '10005',
 '+59': '10002',
 '+64': '10003',
 '+94': '10001'}

record_mapping()

此功能将记录与映射相关联(上面的两个功能)。它通过遍历记录,提取其电话号码及其扩展名,并使用为映射创建的字典来向我们返回您要查找的代码来实现。

要查找该分机,它是通过反转电话号码并删除10位数字,然后再次将其反转来实现的。假设所有数字的长度均为10(可以通过number_length变量输入),而扩展名可以为n个数字,则该功能应该可以使用。

使函数的输入成为上述函数的输出,我们具有:

{'+127159721922': '10006',
 '+2027159721922': '10005',
 '+5959274929424': '10002',
 '+9427159721922': '10001'}
© www.soinside.com 2019 - 2024. All rights reserved.