我想使用https://pypi.org/project/word2number/中的word2numberfromhttps://pypi.org/project/word2number/将文本文件中的单词转换为数字,并将其转换为另一个文件作为输出。在
有一个类似的程序可以把数字转换成单词。那么我该如何处理这个程序来适应我的情况呢。在
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)
当然还有一种更为python式的方法来实现这一点,但是在这里,您需要用库中的函数调用替换word2number,其中参数是字符串。同样,这将跳过换行符并生成一个大行。在
相关问题 更多 >
编程相关推荐