简体   繁体   English

数据管道运行时出现ValueError Python function

[英]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.

相关问题 从云功能启动数据流管道时出错 - Error when launching dataflow pipeline from cloud function ValueError:安装 DBtypes 以使用此 function - ValueError: install DBtypes to use this function 如何为 Apache Beam/Dataflow 经典模板(Python)和数据管道实现 CI/CD 管道 - How to implement a CI/CD pipeline for Apache Beam/Dataflow classic templates (Python) & data pipelines 使用 gitlab-ci.yml 管道测试 python 程序时出现问题 - Problem using MPI in a python program when testing it with a gitlab-ci.yml pipeline cURL command to GitLab API in GitLab pipeline succeeds with [0 bytes data] when cURL contains variable - cURL command to GitLab API in GitLab pipeline succeeds with [0 bytes data] when cURL contains variable Gitlab 运行 gitlab 管道时未注入 CI CD 变量 - Gitlab CI CD variable are not getting injected while running gitlab pipeline Composer/Airflow - 在运行 DAG/管道时添加依赖项? - Composer/Airflow - Adding a dependency while running a DAG/pipeline? gitlab 作业正在运行,即使计划管道中没有更改 - gitlab job is running even if there is no changes in the schedule pipeline 测试没有在我的 gitlab-ci 管道中运行 - Tests are not running in my gitlab-ci pipeline 使用 PubSub 在本地运行 java 数据流管道 - Running java dataflow pipeline locally with PubSub
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM