2

I have a file that contains numbers and letters and I want to replace all the numbers to words (for example, 8 is converted to eight). Well I just don't get a way to check if there is numbers on my file. What is the best effective way to do this?

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
Chloe
  • 41
  • 1
  • 2

4 Answers4

10

The following should work, it reads an input file, uses a regex to find all of the numbers, and the num2words library to replace each with text:

import re
import num2words


with open('input.txt') as f_input:
    text = f_input.read()

text = re.sub(r"(\d+)", lambda x: num2words.num2words(int(x.group(0))), text)

with open('output.txt', 'w') as f_output:
    f_output.write(text)

The num2words library can be installed using:

pip install num2words

So if your input text file contained:

Today 1000 people wrote 500 words to make 321 questions.

The output would be:

Today one thousand people wrote five hundred words to make three hundred and twenty-one questions.
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
2

This is just to find the numbers present in the file, if you dont know how to replace,use dictionary.

import re
text = open('text_numbers.txt')

for line in text:
    line = line.strip()
    y = re.findall('([0-9]+)',line)
Siva Shanmugam
  • 662
  • 9
  • 19
1

This question was also answered here : replacing text in a file with Python

If you want to discuss the memory user and other topics, this question should do the trick : Search for string in txt file Python

Some people explained the different usage of different function to read a text from a file, but in your case, it look like it's not a huge file so the first link should give you the answer.

Just remember, when you parse a txt file, even "8" is a String.

Community
  • 1
  • 1
Aks
  • 395
  • 3
  • 11
1

You find the numbers using regex and then you can convert the number to words with using num2words package. And simply replace them.

In [1]: from num2words import num2words
In [2]: num2words(54)
Out[2]: u'fifty-four'
In [3]: num2words(8)
Out[3]: u'eight'
Rahul K P
  • 15,740
  • 4
  • 35
  • 52