``` labels={ 0:"call",1:"dislike",2:"fist",3:"four",4:"like", 5:"mute",6:"ok",7:"one",8:"palm",9:"peace", 10:"rock",11:"stop",12:"three",13:"three2",14:"two_up", 15:"no_gesture" } dict1={"ad51dece-2c8f-456e-a040-9e5dff422eea": {"bboxes": [[0.22171847, 0.41037644, 0.25789147, 0.17480812], [0.47935231, 0.89097459, 0.19577848, 0.1076597]], "user_id": "205c3ddc197122a41da690650d6f3269fc66fa56f4a84898c3f0c812e6c9f210", "labels": ["call", "no_gesture"]}, "2c1c20b3-4126-4b86-9ed5-ddb4fd820b5d": {"bboxes": [[0.21939388, 0.46569546, 0.27419086, 0.16701518]], "user_id": "62088fcbda92c4aff1b540f6e3fd5c3dfc337a48f667cc0c27d31752192e367f", "labels": ["call"]}, "911ebcf3-9097-4ca8-a2e7-38842fb8e8cb": {"bboxes": [[0.49697955, 0.5298888, 0.24307513, 0.20547046]], "user_id": "721532c7b273bcec409999a39bb8d7172ce74b7c4c26d1c625c467c8fc8cd3ac", "labels": ["call"]}, "d6254426-d1c8-4528-969c-306dfa443f35": {"bboxes": [[0.46056342, 0.25590303, 0.23161644, 0.22088766]], "user_id": "1063c119315efb0a003c2abb13a5645b0943de98be7b38c3a343f1c9d586a772", "labels": ["call"]}}```创建TXT文件时创建到指定的位置
时间: 2025-05-18 21:59:33 浏览: 26
从你的描述来看,你需要将上述数据保存到一个 TXT 文件,并将其存储在指定位置。以下是完成此任务的一般步骤及代码示例:
### 步骤说明
1. **准备数据**
确保 `labels` 和 `dict1` 数据已准备好。
2. **选择文件路径**
指定你想创建的 TXT 文件所在的目录及其名称(例如:`C:\example\output.txt` 或 `/home/user/output.txt`)。
3. **编写数据到文件**
使用 Python 的 `open()` 函数以写入模式打开目标文件并逐行写入所需的数据内容。
---
### 示例代码
假设我们需要把上面的内容按特定格式存入 TXT 文件:
```python
import os
# 定义 labels 映射表
labels = {
0: "call", 1: "dislike", 2: "fist", 3: "four", 4: "like",
5: "mute", 6: "ok", 7: "one", 8: "palm", 9: "peace",
10: "rock", 11: "stop", 12: "three", 13: "three2", 14: "two_up",
15: "no_gesture"
}
# 输入字典 dict1
data_dict = {
"ad51dece-2c8f-456e-a040-9e5dff422eea": {"bboxes": [[0.22171847, 0.41037644, 0.25789147, 0.17480812], [0.47935231, 0.89097459, 0.19577848, 0.1076597]], "user_id": "205c3ddc197122a41da690650d6f3269fc66fa56f4a84898c3f0c812e6c9f210", "labels": ["call", "no_gesture"]},
"2c1c20b3-4126-4b86-9ed5-ddb4fd820b5d": {"bboxes": [[0.21939388, 0.46569546, 0.27419086, 0.16701518]], "user_id": "62088fcbda92c4aff1b540f6e3fd5c3dfc337a48f667cc0c27d31752192e367f", "labels": ["call"]},
"911ebcf3-9097-4ca8-a2e7-38842fb8e8cb": {"bboxes": [[0.49697955, 0.5298888, 0.24307513, 0.20547046]], "user_id": "721532c7b273bcec409999a39bb8d7172ce74b7c4c26d1c625c467c8fc8cd3ac", "labels": ["call"]},
"d6254426-d1c8-4528-969c-306dfa443f35": {"bboxes": [[0.46056342, 0.25590303, 0.23161644, 0.22088766]], "user_id": "1063c119315efb0a003c2abb13a5645b0943de98be7b38c3a343f1c9d586a772", "labels": ["call"]}
}
# 设置目标文件路径
file_path = r'C:\your_folder\output_data.txt'
# 写入文本文件
with open(file_path, 'w', encoding='utf-8') as file:
for key, value in data_dict.items():
# 将每个键值对转换成字符串形式
line_content = f"{key}: {value}\n"
file.write(line_content)
print(f"Data has been successfully written to the file at location: {os.path.abspath(file_path)}")
```
#### 输出解释:
运行以上脚本后会生成包含所有信息的一个 TXT 文件,每条记录对应一行。
---
###
阅读全文
相关推荐


















