我的数据有错字。特别是,我需要将pandas DataFrame的列值从字符串转换为float。但是,简单的 constructor(message: string) {
this.greeting = message;
}
@validate
greet(@required name: string) {
return "Hello " + name + ", " + this.greeting;
}
无法引发此错误:
float(value)
因此,我应用正则表达式删除所有非数字:
"could not convert string to float: '33q.200088'"
但是,在以下情况下,我得到了错误re.sub(r"\D", "", num)
:
TypeError: expected string or bytes-like object
重要提示:我想保持num = -97.94111109999999
的标志。
答案 0 :(得分:1)
此代码应使其起作用:
---
version: '2'
services:
zookeeper:
image: confluentinc/cp-zookeeper:5.0.0
hostname: zookeeper
ports:
- '32181:32181'
environment:
ZOOKEEPER_CLIENT_PORT: 32181
ZOOKEEPER_TICK_TIME: 2000
extra_hosts:
- "moby:127.0.0.1"
kafka:
image: confluentinc/cp-kafka:5.0.0
hostname: kafka
ports:
- '9092:9092'
- '29092:29092'
depends_on:
- zookeeper
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:32181
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092
KAFKA_AUTO_CREATE_TOPICS_ENABLE: "false"
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
extra_hosts:
- "moby:127.0.0.1"
schema-registry:
image: confluentinc/cp-schema-registry:5.0.0
hostname: schema-registry
depends_on:
- zookeeper
- kafka
ports:
- '8081:8081'
environment:
SCHEMA_REGISTRY_HOST_NAME: schema-registry
SCHEMA_REGISTRY_KAFKASTORE_CONNECTION_URL: zookeeper:32181
extra_hosts:
- "moby:127.0.0.1"
答案 1 :(得分:1)
num
是签名的float
类型,而不是string
或bytes
re.sub("[a-zA-Z]", "", str(num))
这将返回一个保留-
和.
的stringType
您应该可以将其转换为float