我正在使用 numpy 從 csv 檔案中檢索資料,它包含 3 列資料:offer_id、sms_limit、sms_price。我想添加驗證:
- offer_id - 只有正整數
- sms_limit - 只有正整數
- sms_price - 正浮點數。
我嘗試撰寫自己的驗證器,如下所示:
def int_validator(x):
if str(x).isdigit():
return x
raise ValueError('Invalid choice please use positive integer number')
pd.read_csv(
converters={'offer_id': int_validator, 'sms_limit': int, 'sms_price': int},
encoding='utf-8',
engine='python',
)
但它根本不起作用:(
僅當我使用 int 時才有效
pd.read_csv(
converters={'offer_id': int, 'sms_limit': int, 'sms_price': int},
encoding='utf-8',
engine='python',
)
但這不是我要找的。此外,如果我在 sms_limit 或 sms_price 中鍵入字串,則它僅適用于列 offer_id 沒有驗證。smb 可以解釋如何撰寫我的驗證器以及為什么只有第一列接受 int 轉換?
uj5u.com熱心網友回復:
這是一個正確檢查前兩列是否包含正整數以及最后一列是否包含正浮點數的解決方案。
# This uses a try-except block to see if the given value is an integer,
# and an if-else block to see if the value is >= 0.
# Change the sign to > 0 if you want strictly positive values.
def int_validator(x):
try:
# A funny little quirk of python: If you have something like x = "7.0", then int(x) returns an error even though int(float(x)) does not.
x = int(float(x))
if x >= 0:
return x
else:
raise ValueError('Invalid choice for {}. Please use positive integer number'.format(x))
except:
raise ValueError('Invalid choice for {}. Please use positive integer number'.format(x))
# This does something similar to the int_validator, but checks if it's a float instead.
def float_validator(x):
try:
x = float(x)
if x >= 0:
return x
else:
raise ValueError('Invalid choice for {}. Please use positive float number'.format(x))
except:
raise ValueError('Invalid choice for {}. Please use positive float number'.format(x))
# Now we apply the validators to all the columns.
pd.read_csv("example.csv",
converters={'offer_id': int_validator, 'sms_limit': int_validator, 'sms_price': float_validator},
encoding='utf-8',
engine='python',
)
如果您有任何問題,請告訴我!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/519643.html
標籤:Python麻木的
