如何从Excel中删除电话号码?

2024-06-06 21:49:21 发布

您现在位置:Python中文网/ 问答频道 /正文

请帮我删除字符串中的电话号码。我有一个excel文件,其中有很多描述。我想删除那些电话号码

说明:

嗨, 可在伊利诺伊州伦巴第市共享住宿。单人房,我只喜欢素食。 如有兴趣,请致电6308283324。 谢谢 默翰


Tags: 文件字符串电话号码excel兴趣住宿素食单人房
2条回答
import re
import pandas as pd
df=pd.DataFrame({'Description':['This is my mobile number 213 321 32111',
                                'another number 123 321 321',
                                'Hi, Shared accommodation is available in Lombard-IL. Single bed room and I preferred Veggie only. Please reach 630 828 3324 if any one interested. Thanks, Mohan']})
for i in range (len(df['Description'])):
    print(re.sub(" \d+", " ",df.loc[i,"Description"]))

此代码将忽略您描述中的所有数字

试试这个:

import openpyxl
import re

wb = openpyxl.load_workbook(r'pathtoexcel')

ph_no=r"\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}"

for sheet_name in wb.sheetnames:
    ws = wb[sheet_name]
    max_col_val = ws.max_column
    max_row_val = ws.max_row
    for cols in range(1,max_col_val + 1):
        for rows in range(1,max_col_val):
            if (ws.cell(rows, cols).value)!=None:
                cl=ws.cell(rows, cols).value
                if re.findall(ph_no, cl):
                    cl=re.sub(ph_no,"",cl)

相关问题 更多 >