[英]Python regex to correct number format
我有包含 1000 个数字的文本文件,格式为 1 3434,00 或 12 4453,00 我需要使用 python 将它们转换为美国/英国本地,以便它们为 13434.00 124453.00。
我将使用什么正则表达式或替换表达式来执行此操作?
您可以使用以下表达式:
x = text.replace(",", ".")
假设所有电话号码都从文件中读取并存储在 phone_number 变量中。 所以,你可以做的是用空替换空格,用点替换逗号。
phone_number = phone_number.replace(' ','')
phone_number = phone_number.replace(',','.')
谢谢。
如果您的“数字”嵌入在文本中,那么您可以尝试以下操作:
import re
test = '''
I have text files containing 1000's of numbers in the format 1 3434,00 or 12 4453,00 I need to turn them into US/UK local using python so that they are 13434.00 124453.00.
What regex expression or replace expression would I use to do this?
'''
def to_number_string(m):
s = m.group()
print(repr(s))
return s.replace(' ', '').replace(',', '.')
r = re.compile(r'\d+[ \d]*,\d*')
print(r.sub(to_number_string, test))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.