ValueTable
汇总
值表是一个灵活的类表对象,由包含各种值的行和列组成。
讨论
ValueTable 对象支持诸如 long、str、bool、double 和 datetime.datetime() 等原生 Python 对象。 这些对象通常更易于使用,并可使代码在不同的计算机上更可靠,特别是对于诸如 GPDate 等类型更是如此,在与字符串进行相互转换时,其需要系统特定的解析。 要从 ValueTable 对象检索 Python 对象,可使用 getTrueValue 和 getTrueRow 函数。 要将 Python 对象添加至或写入到 ValueTable 对象,可使用 setValue 和 addRow 函数。
有关受支持的列数据类型列表,请参阅 数据类型 。
语法
ValueTable({columns})
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
columns (可选) |
向
(默认值为 1) |
Integer |
属性
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
columnCount (只读) |
列数。 |
Integer |
|
rowCount (只读) |
行数。 |
Integer |
方法
addColumns(number_of_columns)
向 ValueTable 对象添加列。 值可以如下所示:
列数为整数。 每列将添加为
GPString数据类型。单个列的数据类型为字符串,例如
"GPString"。多个列的数据类型为字符串列表,例如
["GPDate", "GPLong"]。
addColumns 方法在功能上与 setColumns 方法相当。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
number_of_columns |
The number of columns for the value table. |
Integer |
addRow(value)
向值表添加具有指定值的一行。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
value |
A list of values to be added as a new row. The value argument can be as follows:
|
Object |
exportToString()
将对象导出至其字符串表示。
返回值
| 数据类型 | 说明 |
|---|---|
|
String |
对象的字符串表示。 |
getRow(row)
返回指定索引处的行中的值。 行值将以空格分隔的字符串形式返回。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
row |
The row index position. |
Integer |
返回值
| 数据类型 | 说明 |
|---|---|
|
String |
|
getTrueRow(row)
返回指定索引处的行中的值。 行值将以包含相应 Python 对象的列表形式返回。
列数据类型至 Python 对象映射如下:
整型至
int双精度型至
float布尔型至
bool日期至
datetime.datetime
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
row |
The row index position. |
Integer |
返回值
| 数据类型 | 说明 |
|---|---|
|
Object |
此 Python 列表包含给定行的值作为相应 Python 对象。 |
getTrueValue(row, column)
如果给定列和行索引,则会将值返回为适当的 Python 对象。
列数据类型至 Python 对象映射如下:
整型至
int双精度型至
float布尔型至
bool日期至
datetime.datetime
所有其他列类型的值将返回为字符串。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
row |
The row index position. |
Integer |
|
column |
The column index position. |
Integer |
返回值
| 数据类型 | 说明 |
|---|---|
|
String |
给定列和行的值。 |
getValue(row, column)
将给定列和行中的值返回为字符串。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
row |
The row index position. |
Integer |
|
column |
The column index position. |
Integer |
返回值
| 数据类型 | 说明 |
|---|---|
|
String |
给定列和行中的值。 |
loadFromString(string)
通过已格式化字符串定义 ValueTable 对象。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
string |
The string representation of the object. Within the string, all values are wrapped in single quotes, with each value in a row separated by a space, and each row separated by a semicolon. Each column in the |
String |
removeRow(row)
删除在指定索引处找到的行。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
row |
The index position of the row to remove. |
Integer |
setColumns(number_of_columns)
向 ValueTable 对象添加列。 值可以如下所示:
列数为整数。 每列将添加为
GPString数据类型。单个列的数据类型为字符串,例如
"GPString"。多个列的数据类型为字符串列表,例如
["GPDate", "GPLong"]。
setColumns 方法在功能上与 addColumns 方法相当。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
number_of_columns |
The number of columns for the value table. |
Integer |
setRow(row, value)
更新 ValueTable 对象中的给定行。
注:
value参数以空格分隔。 包含空格的所有值均必须用引号括起。 在以下示例中,要素类和索引值将添加至包含两列的ValueTable对象。
vtab.setRow(0, "'c:/temp/land use.shp' 2")
与其他行操作不同, setRow 仅支持字符串值。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
row |
The index position of the row to update. |
Integer |
|
value |
The value to update in the given row. |
Object |
setValue(row, column, value)
更新给定行或列的值。 值可以是字符串表示或适当的原生 Python 类型。
例如,如果列为数据类型,则可以使用 datetime.datetime 对象或字符串。
# Must be system appropriate string representation of a date
vtab.setValue(0, 1, "2021-06-17")
vtab.setValue(0, 1, datetime.datetime(2021, 6, 17))
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
row |
The row index. |
Integer |
|
column |
The column index. |
Integer |
|
value |
The value to update the given row and column. This can be a string representation or a native Python type ( |
Object |
代码示例
使用 ValueTable 对象以保存 Union 函数的要素类名称和等级。
import arcpy
# Set the workspace. List all of the feature classes in the dataset
arcpy.env.workspace = "c:/data/landbase.gdb/Wetlands"
feature_classes = arcpy.ListFeatureClasses()
# Create the value table for the Analysis toolbox Union function with 2 columns
value_table = arcpy.ValueTable(2)
# Iterate through the list of feature classes
for fc in feature_classes:
# Update the value table with a rank of 2 for each record, except
# for BigBog
if fc.lower() != "bigbog":
value_table.addRow(fc + " 2")
else:
value_table.addRow(fc + " 1")
# Union the wetlands feature classes with the land use feature class to create
# a single feature class with all of the wetlands and land use data
value_table.addRow("c:/data/landbase.gdb/land_use 2")
arcpy.analysis.Union(value_table, "c:/data/landbase.gdb/wetlands_use")
值表可使用多值字符串进行填充,该字符串已作为参数传递到脚本,以方便提取每条记录。
import os
import arcpy
# Set the output workspace
arcpy.env.workspace = arcpy.GetParameterAsText(1)
# Create a value table with 2 columns
value_table = arcpy.ValueTable(2)
# Set the values of the table with the contents of the first argument
value_table.loadFromString(arcpy.GetParameterAsText(0))
# Loop through the list of inputs
for i in range(0, value_table.rowCount):
# Validate the output name for the new workspace
name = value_table.getRow(i)
out_name = arcpy.ValidateTableName(os.path.basename(name))
# Copy the features to the new workspace
arcpy.management.CopyFeatures(name, out_name)
使用原生 Python 类型创建 ValueTable 对象并向其添加值。
import arcpy
import datetime
# Create a value table with 3 columns
value_table = arcpy.ValueTable(["GPLong", "GPBoolean", "GPDate"])
# Set the values of the table with native Python types
value_table.addRow([1, True, datetime.datetime(2004, 12, 19)])
value_table.addRow([2, False, datetime.datetime(2008, 2, 13)])
# Retrieve true Python object from ValueTable
event_date = value_table.getTrueValue(1, 2)