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?
-
1you can create your dictionary `{1: "one", 2: "two"...}` and use the [following answer](http://stackoverflow.com/questions/14156473/can-you-write-a-str-replace-using-dictionary-values-in-python) – JMat Oct 14 '16 at 09:55
-
"best effective way" depends on a lot of things. – Antti Haapala -- Слава Україні Oct 16 '16 at 07:54
4 Answers
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.

- 45,791
- 17
- 81
- 97
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)

- 662
- 9
- 19
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.