[英]ValueError when running Python function in data pipeline
I'm building a data pipeline using Python and I'm running into an issue when trying to execute a certain function. The error message I'm receiving is: ValueError: Could not convert string to float: 'N/A'
我正在使用 Python 构建数据管道,在尝试执行某个 function 时遇到问题。我收到的错误消息是:
ValueError: Could not convert string to float: 'N/A'
Here is the function in question:这是有问题的 function:
def process_data(data):
for item in data:
# Do some processing...
value = float(item[1])
if value > 0:
processed_item = process_item(item)
yield processed_item
I'm calling the function like this:我这样呼叫 function:
data = [('A', '1.5'), ('B', '2.7'), ('C', 'N/A'), ('D', '4.1'), ('E', '5.9')]
processed_data = process_data(data)
Code:代码:
def process_data(data):
for item in data:
# Do some processing...
value = float(item[1])
if value > 0:
processed_item = process_item(item)
yield processed_item
data = [('A', '1.5'), ('B', '2.7'), ('C', 'N/A'), ('D', '4.1'), ('E', '5.9')]
processed_data = process_data(data)
Error message:错误信息:
ValueError: Could not convert string to float: 'N/A'
The expected outcome was to process the items in the data list and yield the processed items if the value of the item was greater than 0.预期结果是处理数据列表中的项目,如果项目的值大于 0,则返回处理后的项目。
The parameter value of float(parameter)
must be a number or a string that can be converted into a floating point number. float(parameter)
的参数值必须是数字或者是可以转为浮点数的字符串。
The value 'N/A' cannot be converted because it is not a number .无法转换值“N/A” ,因为它不是数字。
You could try:你可以试试:
try:
value = float(item[1])
except ValueError:
value = 0
Assuming you want anything that is not a number to become zero, which will then be filtered out by your if value > 0:
statement.假设您希望任何不是数字的东西都变成零,然后将被您的
if value > 0:
语句过滤掉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.