Python 如何将以建筑格式显示的测量值转换为浮点?
我有一个由建筑公司创建和使用的数据库。所有测量值都以如下格式存储:15-3/4“和12'6-3/4” 在Python中有没有办法将这些类型的度量转换为浮点?或者有没有提供此功能的库Python 如何将以建筑格式显示的测量值转换为浮点?,python,regex,measurement,units-of-measurement,Python,Regex,Measurement,Units Of Measurement,我有一个由建筑公司创建和使用的数据库。所有测量值都以如下格式存储:15-3/4“和12'6-3/4” 在Python中有没有办法将这些类型的度量转换为浮点?或者有没有提供此功能的库 同样,如何将浮点转换为上述格式?体系结构转换为浮点: import re regex = re.compile('(\d+\' )*(\d+)-(\d+)\/(\d+)"') regex.sub(lambda m: str((int((m.group(1) or '0').split("'")[0]) * 12)
同样,如何将浮点转换为上述格式?体系结构转换为浮点:
import re
regex = re.compile('(\d+\' )*(\d+)-(\d+)\/(\d+)"')
regex.sub(lambda m: str((int((m.group(1) or '0').split("'")[0]) * 12)
+ int(m.group(2)))
+ ('%.2f' % (int(m.group(3)) / float(m.group(4))))[1:], measurement)
这真的很糟糕,但我已经有一段时间没有使用Python了;我不怀疑有一个更干净的方法可以做到这一点,但它确实很好地处理了脚的缺乏。但是,它总是期望英寸,所以像
12'
这样的测量值必须是12'0”
才能正确解析。考虑下面的自我注释代码。我尽量保持简单
>>> from fractions import Fraction
>>> def Arch2Float(num):
#First Partition from Right so that the Feet and Unit always
#Remains aligned even if one of them is absent
ft,x,inch=num.rpartition("\'")
#Convert the inch to a real and frac part after stripping the
#inch (") identifier. Note it is assumed that the real and frac
#parts are delimited by '-'
real,x,frac=inch.strip("\"").rpartition("-")
#Now Convert every thing in terms of feet which can then be converted
#to float. Note to trap Error's like missing or invalid items, its better
#to convert each items seperately
result=0
try:
result = int(ft.strip("\'"))
except ValueError:
None
#Convert the real inch part as a fraction of feet
try:
result += Fraction(int(real),12)
except ValueError:
None
#Now finally convert the Fractional part using the fractions module and convert to feet
try:
result+=Fraction(frac)/12
except ValueError:
None
return float(result)
酸性试验
>>> print Arch2Float('15-3/4"') # 15-3/4" (without ft)
1.3125
>>> print Arch2Float('12\' 6-3/4"') #12' 6-3/4"
12.5625
>>> print Arch2Float('12\'6-3/4"') #12'6-3/4" (without space)
12.5625
>>> print Arch2Float('3/4"') #3/4" (just the inch)
0.0625
>>> print Arch2Float('15\'') #15' (just ft)
15.0
>>> print Arch2Float('15') #15 (without any ascent considered as inch)
1.25
>>> print Float2Arch(Arch2Float('12\' 6-3/4"'))
12' 6-3/4"
>>> print Float2Arch(Arch2Float('15-3/4"'))
1' 3-3/4"
>>> print Float2Arch(Arch2Float('12\'6-3/4"'))
12' 6-3/4"
>>> print Float2Arch(Arch2Float('3/4"'))
0' 0-3/4"
>>> print Float2Arch(Arch2Float('15\''))
15' 0-0"
>>> print Float2Arch(Arch2Float('15'))
1' 3-0"
>>>
从浮点转换到体系结构将很容易,因为您不必费心解析
>>> def Float2Arch(num):
num=Fraction(num)
ft,inch=Fraction(num.numerator/num.denominator),Fraction(num.numerator%num.denominator)/num.denominator*12
real,frac=inch.numerator/inch.denominator,Fraction(inch.numerator%inch.denominator,inch.denominator)
return '{0}\' {1}-{2}"'.format(ft,real,frac)
酸性试验
>>> print Arch2Float('15-3/4"') # 15-3/4" (without ft)
1.3125
>>> print Arch2Float('12\' 6-3/4"') #12' 6-3/4"
12.5625
>>> print Arch2Float('12\'6-3/4"') #12'6-3/4" (without space)
12.5625
>>> print Arch2Float('3/4"') #3/4" (just the inch)
0.0625
>>> print Arch2Float('15\'') #15' (just ft)
15.0
>>> print Arch2Float('15') #15 (without any ascent considered as inch)
1.25
>>> print Float2Arch(Arch2Float('12\' 6-3/4"'))
12' 6-3/4"
>>> print Float2Arch(Arch2Float('15-3/4"'))
1' 3-3/4"
>>> print Float2Arch(Arch2Float('12\'6-3/4"'))
12' 6-3/4"
>>> print Float2Arch(Arch2Float('3/4"'))
0' 0-3/4"
>>> print Float2Arch(Arch2Float('15\''))
15' 0-0"
>>> print Float2Arch(Arch2Float('15'))
1' 3-0"
>>>
注***重要的是保持浮点数表示为最低分母(英寸)或最高分母(英尺)。我在本例中选择了最高分母英尺。如果您不想降低它,可以将其乘以12
更新以满足四舍五入要求 (不确定这是否优雅,但是否有效)
def Float2Arch(num):
num=分数(num)
英尺,英寸=分数(num.分子/num.分母),分数(num.分子%num.分母)/num.分母*12
实,分数=英寸。分子/英寸。分母,分数(英寸。分子%英寸。分母,英寸。分母)
对于x范围内的i(1,17):
如果分数(分形)<分数(1.0/16*i):断裂
分数=分数(1.0/16*i)
如果分形>=1:
实数+=1
分形=0
返回'{0}\'{1}-{2}'。格式(ft、real、frac)
根据模式的规则性,您可以使用进行解析:
def architectural_to_float(text):
''' Convert architectural measurements to inches.
>>> for text in """15-3/4",12' 6-3/4",3/4",3/4',15',15",15.5'""".split(','):
... print text.ljust(10), '-->', architectural_to_float(text)
...
15-3/4" --> 15.75
12' 6-3/4" --> 150.75
3/4" --> 0.75
3/4' --> 9.0
15' --> 180.0
15" --> 15.0
15.5' --> 186.0
'''
# See http://stackoverflow.com/questions/8675714
text = text.replace('"', '').replace(' ', '')
feet, sep, inches = text.rpartition("'")
floatfeet, sep, fracfeet = feet.rpartition('-')
feetnum, sep, feetdenom = fracfeet.partition('/')
feet = float(floatfeet or 0) + float(feetnum or 0) / float(feetdenom or 1)
floatinches, sep, fracinches = inches.rpartition('-')
inchesnum, sep, inchesdenom = fracinches.partition('/')
inches = float(floatinches or 0) + float(inchesnum or 0) / float(inchesdenom or 1)
return feet * 12.0 + inches
当然有办法。但这些都是带单位的值,因此直接转换为浮点也需要定义隐含的单位。此外,Django只是一个web框架,所以它实际上与此无关。现在您可以创建一个对象,当字符串化时,该对象将生成该格式。然后你可以使用任何模板。谢谢Keith。。。我删除了Django部分。如果数字是分数英寸,如
3/4,这将不起作用“
。也许你只是想坚持下去rpartition@Abhijit编辑为使用整型/压裂分离的rpartition。谢谢。根据@Keith的评论,我会将单位添加到其中,并将浮点转换为一个结构或类,其中包含一个浮点和一个单位。@JesseSmith,您只需将其转换为字符串并添加单位即可。对于exstr(Arch2Float('12\'6-3/4“'))+'ft'
@Abhijit,这种解决方案同样有效。我将@RaymondHettinger与您的Float2Arch
函数结合起来。您如何建议对返回值进行四舍五入,以便像18.33这样的输入返回18'4?”@Clayton,这不是四舍五入。但是,在将值传递给之前,可以通过执行math.ceil(18.33*10)/10
来实现类似的功能Float2Arch@Abhijit,很抱歉给您带来混乱。。。我的意思是1英尺的.33约为4”。因此,如果您Float2Arch('18.33')
预期返回值为18'4“,而不是18'3-24/25”。您建议如何根据指定的增量(即:1/16”,通常是卷尺上最小的增量)对其进行四舍五入?