删除def函数Python 3中.txt文件中的所有数字

删除def函数Python 3中.txt文件中的所有数字,python,python-3.x,Python,Python 3.x,输入:我想获取一个包含内容的.txt文件(参见下面.txt文件中的示例内容) soccerfif@yahoo.com366-44-4444杰佐斯出生于1964年1月12日,杰弗里·普雷斯顿·乔根森出生于1964年1月5日和4日 进程:我想通过Def功能运行它(替换\u编号) 输出:我希望附加通过函数运行的文件以删除所有数字(请参见下面的示例) soccerfif@yahoo.com366-44-4444杰佐斯也于1964年1月12日出生于杰弗里·普雷斯顿·乔根森 这是我的密码: import r

输入:我想获取一个包含内容的.txt文件(参见下面.txt文件中的示例内容)

soccerfif@yahoo.com366-44-4444杰佐斯出生于1964年1月12日,杰弗里·普雷斯顿·乔根森出生于1964年1月5日和4日

进程:我想通过Def功能运行它(替换\u编号)

输出:我希望附加通过函数运行的文件以删除所有数字(请参见下面的示例)

soccerfif@yahoo.com366-44-4444杰佐斯也于1964年1月12日出生于杰弗里·普雷斯顿·乔根森

这是我的密码:

import re
theFile=open("/home/file/wow.txt",'rt', encoding= 'latin-1').read()
words=theFile.split()
def replace_numbers(words):
    new_words=[]
    for word in words:
        pattern= re.compile(" \d+")
        if pattern.match(1):
            new_word= re.sub(pattern, " ", word)
            new_words.append(new_word)
    return new_words

r=replace_numbers(words)
print(r)
运行我运行了这段代码,我得到一个错误:应该是字符串或字节,比如object


关于如何使用def函数对文件执行此操作的任何建议。?

我将这样做:

theFile = open ("wow.txt",'rt', encoding= 'latin-1').read()
words = theFile.split()

def replace_numbers (words) :
    exclude_list = [str (x) for x in range (10)]
    new_words = []
    for word in words :
        if word not in exclude_list :
            new_words.append (word)
    return new_words

r = replace_numbers (words)
print (r)

你能发布准确的回溯消息吗?错误消息表明问题出在哪里?而且您的示例预期输出根本没有任何附加内容。甚至12和1964都是数字。我看到您没有在预期的输出中删除它们?
exclude_list=list('0123456789')
会更具可读性。通过快速测试,速度快了1000倍。