Разбить по категориям (сотовый номер, домашний, без номера)
В строке необходимо найти номер телефона, если сотовый, в столбце Output ячейке присваивается "с", иначе если домашний, присваивается "д", иначе "о". Существуют 2 столбца "А11" и "А55", если c "А11" еще можно что-то придумать (подсчитать через цикл количество цифр и взять начало +79 или 89 (со скобкой не понял что делать)), то с "А55" теряюсь. Проходить циклом в каждой ячейке страшно, так как строк несколько тысяч и все это может надолго затянуться. Может, имеет смысл (и есть возможность) предварительно в экселе отсечь мусорные символы какой-то формулой? Буду рад любому совету. Скидываю эксель и блокнот: https://drive.google.com/drive/folders/1Vd7AH-tTEaQQqdZzdiS7fPnvt4wfWItz?usp=sharing
import pandas as pd
ex1 = pd.read_excel('prim.xlsx')
Для А11 написал очень простой код:
import re
number = '+79603170351'
num = '+7 (884) 147-56-58'
number = ''.join(number.split())
number = ''.join(number.split("-"))
number = ''.join(number.split("+"))
number = ''.join(number.split("("))
number = ''.join(number.split(")"))
print(number)
match = re.findall(r'[78][9].........', number)
print(match)
rematch = re.findall(r'[78][0-8].........', number)
print(rematch)
c = []
if match !=c:
print("с")
elif rematch != c:
print("д")
else:
print("о")
Ответы (1 шт):
Сделал таким образом + вывел нужные номера:
import re
import pandas as pd
import itertools
xlsx = pd.read_excel('reg.xlsx')
c = []
for ind in range (len(xlsx)):
if type(xlsx.iloc[ind]['A11']) is str:
print(ind)
b = ''.join(xlsx.iloc[ind]['A11'].split())
b = ''.join(b.split("+"))
b = ''.join(b.split("("))
b = ''.join(b.split(")"))
b = ''.join(b.split("-"))
match = re.findall(r'[78][9]\d{9}', b)
rematch = re.findall(r'\d{6}', b)
if match !=c:
xlsx.at[ind, 'Output'] = "с"
xlsx.at[ind, 'Input'] = match
elif type(xlsx.iloc[ind]['A55']) is str:
a = ''.join(xlsx.iloc[ind]['A55'].split())
a = ''.join(a.split("+"))
a = ''.join(a.split("("))
a = ''.join(a.split(")"))
a = ''.join(a.split("-"))
a = ''.join(itertools.filterfalse(str.isalpha, a))
sot = re.findall(r'[9]\d{9}', a)
zvon = re.findall(r'd{6}?', a)
if sot != c:
xlsx.at[ind, 'Output'] = "с"
xlsx.at[ind, 'Input'] = sot
elif zvon != c:
xlsx.at[ind, 'Output'] = "д"
xlsx.at[ind, 'Input'] = xlsx.iloc[ind]['A55']
elif rematch !=c:
xlsx.at[ind, 'Output'] = "д"
xlsx.at[ind, 'Input'] = xlsx.iloc[ind]['A11']
else:
xlsx.at[ind, 'Output'] = "о"
else:
xlsx.at[ind, 'Output'] = "о"
elif type(xlsx.iloc[ind]['A55']) is str:
e = ''.join(xlsx.iloc[ind]['A55'].split())
e = ''.join(e.split("+"))
e = ''.join(e.split("("))
e = ''.join(e.split(")"))
e = ''.join(e.split("-"))
e = ''.join(itertools.filterfalse(str.isalpha, e))
sot = re.findall(r'[9]\d{9}', e)
zvon = re.findall(r'\d{6}?', e)
if sot != c:
xlsx.at[ind, 'Output'] = "с"
xlsx.at[ind, 'Input'] = sot
elif zvon != c:
xlsx.at[ind, 'Output'] = "д"
xlsx.at[ind, 'Input'] = xlsx.iloc[ind]['A55']
else:
xlsx.at[ind, 'Output'] = "о"
else:
xlsx.at[ind, 'Output'] = "о"
for ind in range (len(xlsx)):
print(ind)
a = str(xlsx.iloc[ind]['Input'])
a = ''.join(a.split())
a = ''.join(a.split("["))
a = ''.join(a.split("]"))
a = ''.join(a.split("'"))
a = ''.join(a.split("+"))
a = ''.join(a.split("("))
a = ''.join(a.split(")"))
a = ''.join(a.split("-"))
a = ''.join(itertools.filterfalse(str.isalpha, a))
xlsx.at[ind, 'Input'] = a
xlsx.to_excel("end2.xlsx")