資料框中有類似",orange"or的條目"(if needed)",因此代碼存盤","or "("asamount這會破壞代碼的其余部分并給出值錯誤。我不確定我應該改進代碼的哪個階段。
def unit_unify(list_of_texts, unit_dict):
"""Takes a list of strings that contains liquid units, and converts them
into fluid ounces.
"""
str_pattern = make_pattern(units_list)
pattern = fr"(^[\d -/]*)({str_pattern})"
new_list = []
for text in list_of_texts:
re_result = re.search(pattern, text)
if re_result:
amount = re_result.group(1).strip()
unit = re_result.group(2).strip()
if not amount:
amount = "1.0"
if "-" in amount:
ranged = True
else:
ranged = False
amount = re.sub(r"(\d) (/\d)",r"\1\2",amount)
amount = amount.replace("-"," ").replace(" "," ").strip()
amount = re.sub(r"[ ] "," ",amount)
amount_in_dec = frac_to_dec_converter(amount.split(" "))
amount = np.sum(amount_in_dec)
if ranged:
to_oz = (amount*unit_dict[unit])/2
else:
to_oz = amount*unit_dict[unit]
new_list.append(str(round(to_oz,2)))
else:
new_list.append(text)
return new_list
這是分數轉換器:
def frac_to_dec_converter(num_strings):
"""Takes a list of strings that contains fractions and convert them into
floats.
"""
result_list = []
for frac_str in num_strings:
try:
converted = float(frac_str)
except ValueError:
num, denom = frac_str.split('/')
try:
leading, num = num.split(' ')
total = float(leading)
except ValueError:
total = 0
frac = float(num) / float(denom)
converted = total frac
result_list.append(converted)
return result_list
uj5u.com熱心網友回復:
我的第一直覺是在資料框中的物體上添加一些處理,或者只是在您從資料框中讀取時進行處理。
前任。
dataframeVariable = // 將從資料幀中獲取的物體轉換為字串
usableVariable = dataframeVariable.replace(",","")
在示例 ",orange" 中,這會將字串中的 ',' 替換為 '' nothing,從而有效地洗掉它。這同樣適用于您想要洗掉的任何字符。翻譯是另一種選擇。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/467216.html
