Created
September 15, 2016 04:10
-
-
Save jmcurran/1d55e3246147892337d87d5df492ba97 to your computer and use it in GitHub Desktop.
My first Python programme (of any merit)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv, re | |
def readHeightsFile(path): | |
heights = [] | |
with open(path, "r", encoding = "utf-8", errors = "ignore") as csvFile: | |
f1 = csv.DictReader(csvFile) | |
for row in f1: | |
heights.append(row["Height"]) | |
print('Read ', len(heights), ' records') | |
return heights | |
def convertFrac(f): | |
"""Tries to convert a supposed fraction into a decimal number | |
Keyword Arguments: | |
f -- a string contianing the fraction | |
""" | |
patternFrac = re.compile("^ ?([0-9]+)/([0-9]+)$") | |
patternDec = re.compile("^(\.[0-9]+)$") | |
matchFrac = patternFrac.match(f) | |
matchDec = patternDec.match(f) | |
if matchFrac: | |
numerator = float(matchFrac.group(1)) | |
denominator = float(matchFrac.group(2)) | |
if numerator < denominator: | |
return numerator / denominator | |
else: | |
return -1 | |
elif matchDec: | |
d = float(f) | |
if 0 < d < 1: | |
return d | |
else: | |
return -1 | |
else: | |
return -1 | |
def convInchesToMillimetres(x): | |
## clean up some obvious problems before trying to match | |
x = re.sub("[\"\-]", "", x) | |
x = re.sub("^ *$", x, "") | |
x = re.sub("^(5|6)\'([1-9]|10|11)*([13]/[248]).*$", "\1\'\2 \3", x) | |
x = re.sub("^5\'5(7|9)(.*$)", "5\'\1\2", x) | |
if re.match("^ *$", x): | |
return -1 | |
pattern = re.compile("^ *([1-7]) *(ft[.\']?|\'{1,2}|[,;.])? *([0-9]{1,2})? *([\'t]|in\.*)?([ ]+(1/2|([12])/3|(1|3)/4|([15])/6|([1-7])/8|1/12)|(\.?[0-9]{1,3}))?[ +\?`l (2)]*$") | |
m = pattern.match(x) | |
def toMillimetres(feet, inches, frac = 0): | |
"""Converts feet, inches, fractions of an inch to millimeters | |
Keyword Arguments: | |
feet -- the number of feet in the measurement | |
inches -- the number of inches in the measurement | |
frac -- fractions of an inch, either in fraction form or decimal form | |
""" | |
return (feet * 12 + inches + frac) * 25.4 | |
if m: | |
feet = 0 | |
inches = 0 | |
if not(m.group(1) is None): | |
feet = float(m.group(1)) | |
if not(m.group(3) is None): | |
inches = float(m.group(3)) | |
if feet == 0 and inches == 0: | |
print("Couldn't resolve this measurement: ", x) | |
return 0 | |
frac = 0 | |
if m.group(5): | |
frac = convertFrac(m.group(5)) | |
if 0 < frac < 1: | |
return toMillimetres(feet, inches, frac) | |
else: | |
print("Couldn't resolve this fraction: ", m.group(5)) | |
print("Using 0 instead") | |
return toMillimetres(feet, inches) | |
return toMillimetres(feet, inches, frac) | |
else: | |
print("Couldn't resolve this measurement:", h) | |
return 0 | |
myFile = "/users/jcur002/Dropbox/Work/2017/779/crewlist.csv" | |
heights = readHeightsFile(myFile) | |
hmm = [] | |
#for h in heights: | |
# hmm.append(convInchesToMillimetres(h)) | |
convInchesToMillimetres("6 ' 11/2") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment