简体   繁体   中英

replace digits from text

I have a text file I need to replace digits with white space.

I tried splitting the text file first into individual words and then checked if that word is digit or not

   def replace_digits_symbols():
    text_file = open_file()
    for word in text_file:
       for char in word:
         if char.isdigit():
             word.replace(char, " ")
      print(text_file)

it should replace them with white spaces but nothing is happening

The str.replace method simply returns the replaced string without altering the original string in-place, which is why calling word.replace(char, " ") does nothing. You can instead use str.join with a generator expression that iterates through each character in a line and outputs a space instead of the original character if it is a digit:

with open('file') as file:
    for line in file:
        print(''.join(' ' if char.isdigit() else char for char in line))

Here is the complete code for this process,

def helper(text):
    import string
    for digit in string.digits:
        text = text.replace(digit, ' ')
    return text

def ReplaceDigits(fileName):
    output_file = open("processed.txt",'w')
    lineNo = 1
    with open(fileName) as file_ptr:
        for lines in file_ptr:
            print("Processing Line No : {}".format(lineNo))
            lines = helper(lines)
            output_file.write(lines)
            lineNo +=1
ReplaceDigits("test.txt")

test.txt contains

this1is5sample0text
this10is552sample0text
this10is5sample0text
this10is52sample0text
this0is52sample0text

and the result is,

this is sample text
this  is   sample text
this  is sample text
this  is  sample text
this is  sample text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM