正则表达式 - 查找并替换并转换为csv格式

时间:2017-04-09 21:17:31

标签: python regex csv

我有一个包含电话号码的文件,并希望从中创建一个csv文件。

我面临的问题是格式不固定且不易解析。

  • 每行包含一个,两个或三个电话记录。
  • 手机可能启动或不启用(+ xxx),第二部手机可能位于“&”之前或不。

我正在尝试构建一个正则表达式,它可以拆分3组中的每一行,然后查找/替换为预期的格式,但没有成功。

任何人都可以提出正则表达式,可以识别每行的每个组吗?

输入

(+999) 11 762 52 61 (+999) 11 762 41 11
(+999) 44 695 01 76 & 44 695 01 89
(+999) 21 510 02 14 (+999) 21 511 97 98
(+999) 01 05 00 18 67
(+999) 21 552 42 12
(+999) 21 557 86 60 (+999) 21 557 86 72
(+999) 11 873 93 13 & 11 825 59 92
(+999) 15 307 57 15 & 15 307 57 16 & (+999) 11 974 19 57
(+999) 21 551 91 51 (+999) 21 551 91 68
(+999) 21 551 71 71 & 21 551 72 32
(+999) 21 527 30 00 (+999) 21 551 54 89
(+999) 11 621 15 00 (+999) 11 626 20 75
(+999) 21 555 21 60 (+999) 21 555 21 71 (+999) 12 804 76 30
(+999) 11 234 18 96 (+999) 11 234 54 48
(+999) 11 828 35 37 (+999) 11 828 63 76 (+999) 41 363 27 23
(+999) 11 690 03 00 (+999) 11 315 65 38
(+999) 08 32 60 34 65
(+999) 08 32 60 34 65 & (+999) 11 784 46 70 & (+999) 11 784 61 79

预期结果:

(+999) 11 762 52 61, (+999) 11 762 41 11,
(+999) 44 695 01 76, 44 695 01 89,
(+999) 21 510 02 14, (+999) 21 511 97 98,
(+999) 01 05 00 18 67,,
(+999) 21 552 42 12,,
(+999) 21 557 86 60, (+999) 21 557 86 72,
(+999) 11 873 93 13, 11 825 59 92,
(+999) 15 307 57 15, 15 307 57 16, (+999) 11 974 19 57
(+999) 21 551 91 51, (+999) 21 551 91 68,
(+999) 21 551 71 71, 21 551 72 32,
(+999) 21 527 30 00, (+999) 21 551 54 89,
(+999) 11 621 15 00, (+999) 11 626 20 75,
(+999) 21 555 21 60, (+999) 21 555 21 71, (+999) 12 804 76 30
(+999) 11 234 18 96, (+999) 11 234 54 48,
(+999) 11 828 35 37, (+999) 11 828 63 76, (+999) 41 363 27 23
(+999) 11 690 03 00, (+999) 11 315 65 38,
(+999) 08 32 60 34 65,,
(+999) 08 32 60 34 65, (+999) 11 784 46 70, (+999) 11 784 61 79

2 个答案:

答案 0 :(得分:1)

import math

for l in file: 

    nr_of_prefixes = l.count('(+')  # amount of prefixes (+xxx)
    prefixes = nr_of_prefixes * 3 # count the characters of a prefix
    numbers = sum(c.isdigit() for c in l) # amount of numbers in a string
    numbers -= prefixes # remove the prefixes
    telephone_numbers = math.floor(numbers / 8) # number of digits


    l = l.replace(' (+',', (+') # add a , to    (+
    l = l.replace(' &',',')     # replace a & by a comma
    l = l.replace(',,',',')     # replace double ,,  by a single  ,

    # if there where only 2 phone numbers, add an ending comma
    if telephone_numbers < 3:
        l += ","

    # if there was only 1 phone numbers, add an extra comma
    if telephone_numbers < 2:
        l += ","

    # print, or add to a list 
    print(l)

答案 1 :(得分:0)

使用以下正则表达式:((\(\+999\)[\d ]+)|& ([\d ]+))

以下是包含文件内容的示例:

https://regex101.com/r/Q8grqd/1

由regex101代码生成器生成的python代码

import re

regex = r"((\(\+999\)[\d ]+)|& ([\d ]+))"

test_str = ("(+999) 11 762 52 61 (+999) 11 762 41 11\n"
    "(+999) 44 695 01 76 & 44 695 01 89\n"
    "(+999) 21 510 02 14 (+999) 21 511 97 98\n"
    "(+999) 01 05 00 18 67\n"
    "(+999) 21 552 42 12\n"
    "(+999) 21 557 86 60 (+999) 21 557 86 72\n"
    "(+999) 11 873 93 13 & 11 825 59 92\n"
    "(+999) 15 307 57 15 & 15 307 57 16 & (+999) 11 974 19 57\n"
    "(+999) 21 551 91 51 (+999) 21 551 91 68\n"
    "(+999) 21 551 71 71 & 21 551 72 32\n"
    "(+999) 21 527 30 00 (+999) 21 551 54 89\n"
    "(+999) 11 621 15 00 (+999) 11 626 20 75\n"
    "(+999) 21 555 21 60 (+999) 21 555 21 71 (+999) 12 804 76 30\n"
    "(+999) 11 234 18 96 (+999) 11 234 54 48\n"
    "(+999) 11 828 35 37 (+999) 11 828 63 76 (+999) 41 363 27 23\n"
    "(+999) 11 690 03 00 (+999) 11 315 65 38\n"
    "(+999) 08 32 60 34 65\n"
    "(+999) 08 32 60 34 65 & (+999) 11 784 46 70 & (+999) 11 784 61 79")

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches):
    matchNum = matchNum + 1

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))