Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何匹配电话号码前缀?_Python_Database_Shell_Fileparsing_Fileparse - Fatal编程技术网

Python 如何匹配电话号码前缀?

Python 如何匹配电话号码前缀?,python,database,shell,fileparsing,fileparse,Python,Database,Shell,Fileparsing,Fileparse,我正在用电话号码分析数据,我需要与国家和运营商匹配。我已收到以下形式的电话号码前缀的国家/地区和目的地(城市/运营商)映射: Country, Destination, Country Code, Destination Code, Remarks AAA, Some Mobile, 111, "12, 23, 34, 46",Some remarks AAA, Some city A, 111, "55, 56, 57, 51", Some more remarks BBB, Some cit

我正在用电话号码分析数据,我需要与国家和运营商匹配。我已收到以下形式的电话号码前缀的国家/地区和目的地(城市/运营商)映射:

Country, Destination, Country Code, Destination Code, Remarks
AAA, Some Mobile, 111, "12, 23, 34, 46",Some remarks
AAA, Some city A, 111, "55, 56, 57, 51", Some more remarks
BBB, Some city B, 222, "234, 345, 456", Other remarks
这里的数据是虚拟数据,但实际数据的形式相同。“目的地代码”列中有很多值。所以我想把这个文件转换成适合在数据库中使用的格式

我想到的是将其转换为如下所示的形式:

Country, Destination, Combined Code, Remarks
AAA, Some Mobile, 11112, Some remarks
AAA, Some Mobile, 11123, Some remarks
AAA, Some Mobile, 11134, Some remarks
AAA, Some Mobile, 11146, Some remarks
etc..
这将使我能够创建一个更简单的映射表。处理此类数据的最佳方法是什么?如何用bashshell脚本或python编写用于此转换的代码

>>> data = [['Country', 'Destination', 'Country Code', 'Destination Code', 'Remarks'],
... ['AAA', 'Some Mobile', '111', '12, 23, 34, 46','Some remarks'],
... ['AAA', 'Some city A', '111', '55, 56, 57, 51', 'Some more remarks'],
... ['BBB', 'Some city B', '222', '234, 345, 456', 'Other remarks']]
>>> 
>>> op=[data[0]]
>>> for i in data[1:]:
...    for j in i.pop(3).split(','):
...       op.append([k+j.strip() if i.index(k)==2 else k for k in i])
... 

>>> for i in op:
...    print i
... 
['Country', 'Destination', 'Country Code', 'Destination Code', 'Remarks']
['AAA', 'Some Mobile', '11112', 'Some remarks']
['AAA', 'Some Mobile', '11123', 'Some remarks']
['AAA', 'Some Mobile', '11134', 'Some remarks']
['AAA', 'Some Mobile', '11146', 'Some remarks']
['AAA', 'Some city A', '11155', 'Some more remarks']
['AAA', 'Some city A', '11156', 'Some more remarks']
['AAA', 'Some city A', '11157', 'Some more remarks']
['AAA', 'Some city A', '11151', 'Some more remarks']
['BBB', 'Some city B', '222234', 'Other remarks']
['BBB', 'Some city B', '222345', 'Other remarks']
['BBB', 'Some city B', '222456', 'Other remarks']
更新问题的解决方案:

>>> data = [['Country', 'Destination', 'Country Code', 'Destination Code', 'Remarks'],
...  ['AAA', 'Some Mobile', '111', '12, 23, 34, 46','Some remarks'],
...  ['AAA', 'Some city A', '111', '55, 56, 57, 51', 'Some more remarks'],
...  ['BBB', 'Some city B', '222', '234, 345, 456', 'Other remarks']]
>>>  
>>> op=[data[0]]
>>> for i in data[1:]:
...    for id,j in enumerate(i.pop(3).split(',')):
...       k=i[:]
...       k.insert(3,i[2]+j.strip())
...       op.append(k)
... 
>>> for i in op:
...    print i
... 
['Country', 'Destination', 'Country Code', 'Destination Code', 'Remarks']
['AAA', 'Some Mobile', '111', '11112', 'Some remarks']
['AAA', 'Some Mobile', '111', '11123', 'Some remarks']
['AAA', 'Some Mobile', '111', '11134', 'Some remarks']
['AAA', 'Some Mobile', '111', '11146', 'Some remarks']
['AAA', 'Some city A', '111', '11155', 'Some more remarks']
['AAA', 'Some city A', '111', '11156', 'Some more remarks']
['AAA', 'Some city A', '111', '11157', 'Some more remarks']
['AAA', 'Some city A', '111', '11151', 'Some more remarks']
['BBB', 'Some city B', '222', '222234', 'Other remarks']
['BBB', 'Some city B', '222', '222345', 'Other remarks']
['BBB', 'Some city B', '222', '222456', 'Other remarks']

谢谢,这真的很酷而且很有效。还有一个要求。如何将国家代码保持在第三行,并将“组合代码”保留在第四列?比如['BBB','Some city B','222','222456','Other Comments'.@sfactor,我已经根据你的要求更新了我的答案。希望它能解决问题,如果能解决,请接受答案。