> For the complete documentation index, see [llms.txt](https://testindata.gitbook.io/dataset/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://testindata.gitbook.io/dataset/python-sdk/huo-qu-shu-ju-yu-geng-xin-shu-ju.md).

# 获取数据与更新数据

全局的TDA对象提供了数据检索的功能，方便您按照条件检索查询您已经同步到数据集管理系统的数据。

### 分页检索数据

```bash
tda.GetData(offset=0, limit=100, metaKey="", metaValue="", label="", sensor="")
```

| 参数       | 类型     | 必填 | 说明                                           |
| -------- | ------ | -- | -------------------------------------------- |
| offset   | int    | 否  | 检索数据的起始位置，默认为0，大于0                           |
| limit    | int    | 否  | 检索数据的数量，默认为1000，最大为1000                      |
| metaData | dict   | 否  | 按meta信息检索数据，检索时会按并列(and)关系从上传时设置的meta信息中匹配键值 |
| label    | string | 否  | 按lebel检索，目前仅支持按单个label检索                     |
| sensor   | string | 否  | 按sensor检索                                    |

{% hint style="info" %}
以上所有检索条件均为并列(and)关系
{% endhint %}

返回结果为一个dict

| 字段    | 类型   | 说明                                                           |
| ----- | ---- | ------------------------------------------------------------ |
| total | int  | 本次检索的数据总量                                                    |
| files | list | 元素为testindata.dataset.updatefile.UpdateFile 的list，每一个元素为一个文件 |

**分页检索更类似文件列表的检索，所以检索结果无标注结果，要取得标注结果需按fid检索数据。**&#x6837;例代码：

```python
page = 0
limit = 100
while True:
    offset = page * limit
    fileData = tda.GetData(offset, limit)
    if len(fileData["files"]) <= 0:
        break
    page += 1
    for file in fileData["files"]:
        #文件的相关信息
        print(file.fid)#文件fid
        print(file.name)#文件basename
        print(file.referId)#文件ref_id
        print(file.metaData)#文件meta信息
        print(file.md5)#文件md5
        print(file.path)#文件oss路径
        print(file.url)#文件临时可访问地址
        print(file.size)#文件大小
        print(file.frameId)#融合数据的frameId
        print(file.sensor) #融合数据的sensor
        print(file.createAt)#文件上传时间，unix 时间戳
        print(file.updateAt)#文件上次更新时间， unix 时间戳  
        
        print(file.ToList())#将数据转为list,方便使用
        
        #下载这个文件
        res = requests.get(file.url)
        with open('/path/to/saveFile', 'wb') as f:
            f.write(res.content)

        #删除这个文件及相关信息
        print(file.Delete())
                       
```

###

### 按fid或者referId(ref\_id)检索数据

```bash
tda.GetFileAndLabel(fid="", referId="")
```

| 参数      | 类型     | 必填 | 说明                                                                 |
| ------- | ------ | -- | ------------------------------------------------------------------ |
| fid     | string | 否  | 按fid检索数据                                                           |
| referId | string | 否  | 按ref\_id检索，如果上传数据或者更新数据时同一数据集内传入了多个相同的ref\_id，则本次检索仅会检索出第一个符合条件的结果 |

{% hint style="info" %}
fid 检索优先级会高于ref\_id，当fid和referId同事设置时，会优先按fid检索
{% endhint %}

返回结果为testindata.dataset.updatefile.UpdateFile 的对象，该对象包含本次检索文件的所有信息。

```bash
file = tda.GetFileAndLabel(fid="fs_wB2HyED8iCxm9EqrUE0_")
print(file.anotations.labels)#该文件的所有标注结果
print(file.fid)#文件fid
print(file.name)#文件basename
print(file.referId)#文件ref_id
print(file.metaData)#文件meta信息
print(file.md5)#文件md5
print(file.path)#文件oss路径
print(file.url)#文件临时可访问地址
print(file.size)#文件大小
print(file.frameId)#融合数据的frameId
print(file.sensor) #融合数据的sensor
print(file.createAt)#文件上传时间
print(file.updateAt)#文件上次更新时间   

print(file.ToList())#将数据转为list,方便您使用

#下载这个文件
res = requests.get(file.url)
with open('/path/to/saveFile', 'wb') as f:
    f.write(res.content)

#删除这个文件及相关信息
print(file.Delete())
```

###

### 更新数据

我们可以利testindata.dataset.updatefile.UpdateFile 对象该对该文件的数据做更新操作。目前更新仅支持以下字段：

| 字段         | 类型     | 说明                                                             |
| ---------- | ------ | -------------------------------------------------------------- |
| referId    | string | 文件的ref\_id，同一数据集如果传入多个相同的ref\_id，按该字段检索时仅会返回第一个符合要求的结果         |
| metaData   | dict   | 文件的meta信息，修改时会覆盖原有结果                                           |
| name       | string | 文件basename                                                     |
| sensor     | string | 点云融合数据的传感器名称，填入的值为枚举值，pointcloud为点云，camera\_*0 至 camera\_9为相机* |
| frameId    | string | 点云融合数据帧id，用于关联不同传感器的可视化，最大长度64                                 |
| anotations | object | 该文件的标注结果，需要调用AddBox2D(), AddLine() 等方法操作添加数据，修改时会覆盖原有结果        |

示例代码：

```bash
updateFile = tda.GetFileAndLabel(fid="fs_wB2HyED8iCxm9EqrUE0_")

updateFile.referId = "test_refer_id"
updateFile.metaData = {
    "key1":"test_val"
}

updateFile.name = "23333.jpg"

updateFile.sensor = "camera_1"
updateFile.frameId = "test_frame_id"

box = {
    "x": 10,
    "y": 10,
    "width": 100,
    "height": 100,
}

label = "myTestLabelName"

attr = {
    "attrKey1":"attrVal1",
    "attrKey2":"attrVal2",
    "attrKey3":"attrVal3",
}

updateFile.AddBox2D(box, label=label, attrs=attr)

print(updateFile.Update())
```

方法返回为一个dict

```bash
{'file_succ': 1, 'file_fail': 0, 'anotations_succ': 1, 'anotations_fail': 0}
```

| 字段               | 类型  | 说明            |
| ---------------- | --- | ------------- |
| file\_succ       | int | 文件信息成功更新的文件数量 |
| file\_succ       | int | 文件信息更新失败的数量   |
| anotations\_succ | int | 标注结果更新成功数量    |
| anotations\_fail | int | 标注结果更新失败数量    |

{% hint style="info" %}
**对标注结果的更新是一个覆盖更新的过程，最终仅会保留本次更新的结果，请谨慎操作**
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://testindata.gitbook.io/dataset/python-sdk/huo-qu-shu-ju-yu-geng-xin-shu-ju.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
