read_spreadsheet - Excel、CSV 行列读取

更新时间:
复制 MD 格式

read_spreadsheet 读取 XLSX、XLS 或 CSV 文件中工作表的物理行列坐标及单元格内容,保留公式、日期和可用的格式线索,结果写入 sheets 列。单元格值使用带类型标签的固定结构编码,分别保留字符串、数字、布尔与日期类型。

调用方式

通过 ds.mmops.read_spreadsheet() 调用,接收并返回 Ray Dataset。算子读取工作表的物理行列坐标及单元格内容,保留公式、日期和可用的格式线索。单元格值使用带类型标签的标量(TaggedScalar)固定结构,分别保留字符串、数字、布尔与日期类型,各字段说明和读取示例请参见本文。使用前请确认已满足离线算子概述中的前提条件。

输入数据

每行输入一个文件,每行输入对应一行结果(1→1)。数组默认保留在一行内,仅在显式使用 Ray 的 flat_map 时展开。

列名

类型

说明

document_uri

str

由 document_column 指定的每行输入文档地址,每行一个文件。示例使用此列名。

参数说明

*_column 参数指定从每行读取数据的列名;其他参数是本次调用的全局常量。

参数

类型

默认值

说明

document_column

str

"document_uri"

读取每行文档地址的列名。值为执行节点可访问的文件 URI 或共享路径。

config

dict

无(必填)

文件格式、工作表选择与读取限制。

config.format

str

无(必填)

文件格式,取值为 xlsx、xls 或 csv。

config.sheet_names

list[str] | None

None

需要读取的工作表名称。为 None 时读取全部工作表;指定的名称不存在时报错。

config.sheet_name

str

"Sheet1"

仅适用于 CSV:结果中的工作表名称。

config.encoding

str | None

None

仅适用于 CSV:显式指定的文本编码。省略时自动尝试。

config.delimiter

str | None

None

仅适用于 CSV:显式指定的单字符分隔符。省略时自动探测,探测失败时回退为逗号。

config.max_file_bytes

int

268435456

输入文件的大小上限,单位为字节,默认为 256 MiB,取值为正整数。

config.limits.max_rows

int

1000000

所选工作表的累计行数预算,取值为正整数。

config.limits.max_cells

int

5000000

所选工作表的累计单元格数预算,取值为正整数。

keep_columns

list[str] | None

None

需要保留的原输入列名,默认不透传任何原输入列。与任一标准结果列重名时报错,需先用 Ray Data 原生的 rename_columns 改名。

batch_size

int

1

Ray 每批输入的行数,取值为正整数,不是单行内数组的长度。

concurrency

int

1

Ray 执行并发度,取值为正整数。

num_cpus

float

1

每个执行任务申请的 Ray CPU 资源,取值须大于 0。

输出数据

每行输入对应一行结果。默认仅返回下列顶层结果列,嵌套字段保留在对应的 list 或 struct 内。keep_columns 只追加指定的输入列,与结果列重名时报错,不会覆盖任何一方。

列名或嵌套路径

类型

说明

sheets

list[struct]

本文件中所选的工作表。每个输入文件仍对应一行。

sheets[].sheet_name

str

工作表名称。

sheets[].rows

list[struct]

保留物理行号的行记录。

sheets[].rows[].row_number

int

物理行号,从 1 开始。

sheets[].rows[].cells

list[struct]

稀疏单元格列表。

sheets[].rows[].cells[].column_number

int

物理列号,从 1 开始。

sheets[].rows[].cells[].value

TaggedScalar | None

原单元格值的固定类型编码。缺少值字段时,外层为 null。

sheets[].rows[].cells[].formula

str | None

公式字符串,可用时保留。

sheets[].rows[].cells[].cached_value

TaggedScalar | None

已有公式缓存值的固定类型编码,不重新计算。字段缺失时,外层为 null。

sheets[].rows[].cells[].value_type

str | None

可用的日期格式线索。实际值类型以 TaggedScalar.type 为准。

sheets[].rows[].cells[].raw_value

TaggedScalar | None

原始值的固定类型编码,可用时保留。字段缺失时,外层为 null。

sheets[].rows[].cells[].has_background

bool | None

Excel 中可用的背景样式线索。

带类型标签的标量(TaggedScalar)

本文中的 TaggedScalar 是包含 5 个固定字段的 struct。所有字段始终存在,只有与 type 对应的值字段有值,其余值字段为 null。该结构保留原值的类型,不会将所有单元格转换为 JSON 文本。

字段

Arrow 类型

说明

TaggedScalar.type

string(不可为 null)

取值为 null、bool、int、float、string、date、datetime、time 或 bigint。

TaggedScalar.bool_value

bool | None

仅在 type="bool" 时使用,false 是有效值。

TaggedScalar.int_value

int64 | None

仅在 type="int" 时使用,0 是有效值。

TaggedScalar.float_value

float64 | None

仅在 type="float" 时使用,值必须为有限数。

TaggedScalar.text_value

string | None

在 type 为 string、date、datetime、time 或 bigint 时使用,空字符串是有效的 string 值。

各 type 取值的编码规则如下。

type

编码规则

null

显式的 JSON null,四个值字段全部为 null。

bool

保存在 bool_value 中,布尔值不会被当作整数。

int

有符号 int64,取值范围为 [-9223372036854775808, 9223372036854775807],保存在 int_value 中。

float

有限的 float64,保存在 float_value 中。NaN 和 Infinity 不能放入此字段。

string

原字符串保存在 text_value 中,不再额外添加 JSON 引号。

date

ISO 格式的日期保存在 text_value 中,例如 2026-10-20。

datetime

ISO 格式的日期时间保存在 text_value 中,保留原有的时区信息,不推断时区。

time

ISO 格式的时间保存在 text_value 中,保留原有的时区信息,不推断时区。

bigint

超出 int64 范围的整数以规范的十进制文本保存在 text_value 中,可带负号,不含多余的前导零或指数,例如 9223372036854775808,不会转换为浮点数。

缺少某个值字段时,该字段的外层 struct 为 null;字段存在且值为 JSON null 时,必须保留 type="null" 的 struct。四个值字段均为 null 并不能单独表示值缺失,必须结合外层 struct 和 type 判断。标签不合法、值写入错误的字段、多个字段同时有值、int 越界或 float 非有限数时,编码均会报错。

仅当来源数据的真实类型或 value_type 等可靠元数据表明为日期时,才使用日期类标签;外观符合 ISO 格式的普通字符串仍为 string,不会被推断为日期。在已有转换中丢失且没有可靠元数据的类型不会被恢复。

以下示例仅演示值的编码方式,并非算子的实际返回数据。

{
  "missing_value_field": null,
  "explicit_json_null": {
    "type": "null", "bool_value": null, "int_value": null,
    "float_value": null, "text_value": null
  },
  "integer_value": {
    "type": "int", "bool_value": null, "int_value": 3,
    "float_value": null, "text_value": null
  },
  "large_integer_value": {
    "type": "bigint", "bool_value": null, "int_value": null,
    "float_value": null, "text_value": "9223372036854775808"
  }
}

稀疏工作表仅保留实际读出的单元格,不会为未出现的空白物理单元格补充记录,也不会批量填入 type=null 的值。读取、区域检测(detect_table_regions)和行解析(parse_table_rows)使用同一 TaggedScalar 结构,规则基于解码后的值判断字符串、数值与日期,不能将整个标签 struct 当作表头字符串或数值。

使用示例

说明

示例中的 /shared 是集群所有 Worker 节点均可读写的共享挂载目录,需事先放置输入文件。

import ray
from mmops.ray import enable_accessor

ray.init()
enable_accessor()

ds = ray.data.from_items([{"document_uri": "/shared/input/example.xlsx"}])
result = ds.mmops.read_spreadsheet(
    document_column="document_uri",
    config={"format": "xlsx"},
)
result.select_columns(['sheets']).write_parquet(
    "/shared/metadata/read_spreadsheet"
)

write_parquet() 保存结果列元数据。本算子不写文件产物,无需指定 output_dir。

示例返回结果

以下为与上述调用对应的返回结构示例,文档内容以实际输入为准。算子返回 Dataset,take() 仅取少量行到本地查看;多行结果不保证跨分区的顺序。

假设工作簿包含示例中的两行数据。每个 value 均使用完整的 TaggedScalar 结构,整数 3 保存在 int_value 中。

import json

rows = result.take(1)
print(json.dumps(rows, ensure_ascii=False, indent=2))
[
  {
    "sheets": [
      {
        "sheet_name": "销售",
        "rows": [
          {
            "row_number": 1,
            "cells": [
              {
                "column_number": 1,
                "value": {
                  "type": "string",
                  "bool_value": null,
                  "int_value": null,
                  "float_value": null,
                  "text_value": "商品"
                },
                "formula": null,
                "cached_value": null,
                "value_type": null,
                "raw_value": null,
                "has_background": true
              },
              {
                "column_number": 2,
                "value": {
                  "type": "string",
                  "bool_value": null,
                  "int_value": null,
                  "float_value": null,
                  "text_value": "数量"
                },
                "formula": null,
                "cached_value": null,
                "value_type": null,
                "raw_value": null,
                "has_background": true
              }
            ]
          },
          {
            "row_number": 2,
            "cells": [
              {
                "column_number": 1,
                "value": {
                  "type": "string",
                  "bool_value": null,
                  "int_value": null,
                  "float_value": null,
                  "text_value": "收纳盒"
                },
                "formula": null,
                "cached_value": null,
                "value_type": null,
                "raw_value": null,
                "has_background": false
              },
              {
                "column_number": 2,
                "value": {
                  "type": "int",
                  "bool_value": null,
                  "int_value": 3,
                  "float_value": null,
                  "text_value": null
                },
                "formula": null,
                "cached_value": null,
                "value_type": null,
                "raw_value": null,
                "has_background": false
              }
            ]
          }
        ]
      }
    ]
  }
]

使用限制

  • 读取 XLSX 依赖 openpyxl,读取 XLS 依赖 xlrd,可通过安装 mmops[office] 获取。读取 CSV 不依赖 Excel 引擎。

  • 行列坐标从 1 开始,Excel 中的空白行和空单元格可能被省略。CSV 的行号是记录开始处的物理行号,引号内的换行不会压缩后续坐标。

  • 不重新计算公式;缓存字段缺失与显式 null 按上述规则区分。日期、日期时间与时间分别使用 date、datetime、time 标签并保存 ISO 格式文本,不推断业务时区。CSV 默认依次尝试 UTF-16 BOM、UTF-8、GB18030 和 CP1252 编码。

  • 调用 detect_table_regions 进行区域检测前,请使用 result.flat_map(lambda r: [{"sheet": s} for s in r["sheets"]]) 显式展开为每行一个完整的工作表。需要来源字段时,请在读取阶段通过 keep_columns 指定保留,并在展开时显式复制。

  • 返回的 sheets 中,value、cached_value、raw_value 均采用 TaggedScalar 结构,不同单元格类型不会改变 Arrow struct 的字段集合。