python从字符串中删除电话号码 [英] python remove phone numbers from string

查看:111
本文介绍了python从字符串中删除电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,如下所示:

I have a text file that looks like this:

rgf34 | 9 | 2015-07-20 | hello this is my number 1234567890 
rgf35 | 10 | 2015-07-20 | my number : 123 - 456 -8888 can you check...

这些列用管道分隔,并且每个新数据都在新行上.

The columns are pipe separated and each new data is on a new line.

我如何仅浏览第三列来删除所有PH值? (所有电话号码均为10位数字,我不知道它们是否带有方括号或破折号.)

How can i go through just the third column to remove all ph numbers? (all phone numbers are 10 digits and i do not know if they have brackets or dashes.)

我知道我可以先使用awk来获取第三列中的数据,但对如何做正则表达式感到困惑:

I know i can use awk to just get data in the third column first but am stumped on how to do the regex part:

这是awk的一部分:

awk -F "|" '{print $4}' myfile.txt

预期输出:

rgf34 | 9 | 2015-07-20 | hello this is my number 
rgf35 | 10 | 2015-07-20 | my number : can you check...

推荐答案

如果将其放在a.awk中

If you put this in a.awk

BEGIN {
    FS = OFS = "|"
}
{
    sub(/[0-9].*[0-9]/, "", $4)
    print
}

然后运行

awk -f a.awk foo.txt

您将获得所需的输出.

如果输入为

rgf34 | 9 | 2015-07-20 | hello this is my number 1234567890 
rgf35 | 10 | 2015-07-20 | my number : 123 - 456 -8888 can you check...

输出将是

rgf34 | 9 | 2015-07-20 | hello this is my number
rgf35 | 10 | 2015-07-20 | my number :  can you check...

这篇关于python从字符串中删除电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆