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

时间:2019-11-20 14:11:01

标签: python python-3.x

使用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变量上方,我可以提取+942759721922,我想要将起始数字与'NumberMapping.txt'中的数字进行匹配

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

请帮助并提前感谢。

2 个答案:

答案 0 :(得分:1)

您可以简单地为映射数据和呼叫数据创建字典,然后遍历映射键,如果ISDN以它开头,则打印相应的值。如果您从循环中发现匹配中断,因为没有任何点检查其余映射

call_data = 'A:QEC, B:SQ.O, Date:20181009, Duration:0, RecordClosing:480, A_ISDN:+9427159721922'
number_mappings = """+94:10001
+59:10002
+64:10003
+19:10004
+20:10005
+1:10006"""

#create a dict of the call_data and mappings
call_data_dict = dict((items.split(':') for items in call_data.split(', ')))
number_mapping_dict = dict((item.split(':') for item in number_mappings.splitlines()))

#iterate over the mapping keys to see if our ISDN starts with it.
for key in number_mapping_dict:
    if call_data_dict['A_ISDN'].startswith(key):
        print(f'detected prefix {key} for msisdn {call_data_dict["A_ISDN"]} resulting value is {number_mapping_dict[key]}')
        break  # no point keep checking once we have a match

输出

detected prefix +94 for msisdn +9427159721922 resulting value is 10001

答案 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
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"""
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[:-1]] = 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)

输出:

{'+127159721922': '10006',
 '+2027159721922': '10005',
 '+5959274929424': '10002',
 '+9427159721922': '10001'}

澄清

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'}