Initial commit: ListToMAP v1.0
This commit is contained in:
42
.gitignore
vendored
Normal file
42
.gitignore
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
# Python
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
*.so
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
|
||||
# Virtual Environment
|
||||
venv/
|
||||
ENV/
|
||||
env/
|
||||
|
||||
# IDE
|
||||
.vscode/
|
||||
.idea/
|
||||
*.swp
|
||||
*.swo
|
||||
*~
|
||||
|
||||
# Test outputs
|
||||
Test/reports/*
|
||||
Test/logs/*
|
||||
!Test/reports/.gitkeep
|
||||
!Test/logs/.gitkeep
|
||||
|
||||
# Output files
|
||||
output.csv
|
||||
0
Code/config/.gitkeep
Normal file
0
Code/config/.gitkeep
Normal file
50
Code/docs/README.md
Normal file
50
Code/docs/README.md
Normal file
@@ -0,0 +1,50 @@
|
||||
# ListToMAP
|
||||
|
||||
将 CSV 中的点数据转换为二维网格分布的 CSV 表格。
|
||||
|
||||
## 功能说明
|
||||
|
||||
1. **输入格式**:CSV 文件,包含 6 列
|
||||
- A 列:序号
|
||||
- B 列:名称
|
||||
- C 列:X 位置
|
||||
- D 列:Y 位置
|
||||
- E 列:X 大小
|
||||
- F 列:Y 大小
|
||||
|
||||
2. **处理逻辑**:
|
||||
- 以最小的 X 大小/Y 大小 为刻度单位
|
||||
- 根据 X/Y 位置除以刻度后四舍五入,确定网格坐标
|
||||
- 左下角为坐标原点
|
||||
|
||||
3. **输出格式**:
|
||||
- 每个单元格内容:`序号 + 换行 + 名称`
|
||||
- 输出为新的 CSV 文件
|
||||
|
||||
## 使用方法
|
||||
|
||||
```bash
|
||||
cd ListToMAP
|
||||
python3 main.py
|
||||
```
|
||||
|
||||
## 示例
|
||||
|
||||
输入 `input.csv`:
|
||||
```
|
||||
1,点 A,0,0,10,10
|
||||
2,点 B,10,0,10,10
|
||||
3,点 C,20,0,10,10
|
||||
4,点 D,0,10,10,10
|
||||
5,点 E,10,10,10,10
|
||||
```
|
||||
|
||||
输出 `output.csv`(用 Excel 打开可见换行效果):
|
||||
```
|
||||
4,点 D,5,点 E
|
||||
1,点 A,2,点 B
|
||||
```
|
||||
|
||||
## 依赖
|
||||
|
||||
仅使用 Python 标准库(csv 模块),无需安装任何第三方库。
|
||||
107
Code/src/main.py
Normal file
107
Code/src/main.py
Normal file
@@ -0,0 +1,107 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
ListToMAP - 将 CSV 中的点数据转换为二维网格分布的 CSV 表格
|
||||
"""
|
||||
|
||||
import csv
|
||||
|
||||
|
||||
def read_input_csv(filepath):
|
||||
"""读取输入 CSV 文件,返回点数据列表"""
|
||||
points = []
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
reader = csv.reader(f)
|
||||
for row in reader:
|
||||
if len(row) >= 6:
|
||||
points.append({
|
||||
'index': row[0],
|
||||
'name': row[1],
|
||||
'x_pos': float(row[2]),
|
||||
'y_pos': float(row[3]),
|
||||
'x_size': float(row[4]),
|
||||
'y_size': float(row[5])
|
||||
})
|
||||
return points
|
||||
|
||||
|
||||
def find_min_sizes(points):
|
||||
"""找到最小的 X 大小和 Y 大小作为刻度"""
|
||||
min_x_size = min(p['x_size'] for p in points)
|
||||
min_y_size = min(p['y_size'] for p in points)
|
||||
return min_x_size, min_y_size
|
||||
|
||||
|
||||
def calculate_grid_coords(points, min_x_size, min_y_size):
|
||||
"""计算每个点的网格坐标"""
|
||||
for p in points:
|
||||
# 位置除以刻度,四舍五入
|
||||
p['grid_x'] = round(p['x_pos'] / min_x_size)
|
||||
p['grid_y'] = round(p['y_pos'] / min_y_size)
|
||||
return points
|
||||
|
||||
|
||||
def build_grid(points):
|
||||
"""构建二维网格,左下角为原点"""
|
||||
if not points:
|
||||
return [], 0, 0
|
||||
|
||||
# 找到最大网格坐标
|
||||
max_x = max(p['grid_x'] for p in points)
|
||||
max_y = max(p['grid_y'] for p in points)
|
||||
|
||||
# 创建网格(行数为 max_y+1,列数为 max_x+1)
|
||||
# 由于左下角是原点,grid 的行索引 0 对应最下面一行
|
||||
grid = [['' for _ in range(max_x + 1)] for _ in range(max_y + 1)]
|
||||
|
||||
# 填充网格
|
||||
for p in points:
|
||||
x = p['grid_x']
|
||||
y = p['grid_y'] # y=0 是最下面一行
|
||||
# 格式:序号 + 换行 + 名称
|
||||
grid[y][x] = f"{p['index']}\n{p['name']}"
|
||||
|
||||
# 翻转网格,使输出时第一行是最上面一行(Y 值最大的行)
|
||||
grid_flipped = grid[::-1]
|
||||
|
||||
return grid_flipped, max_x + 1, max_y + 1
|
||||
|
||||
|
||||
def write_output_csv(filepath, grid):
|
||||
"""将网格写入输出 CSV 文件"""
|
||||
with open(filepath, 'w', encoding='utf-8', newline='') as f:
|
||||
writer = csv.writer(f)
|
||||
for row in grid:
|
||||
writer.writerow(row)
|
||||
|
||||
|
||||
def main():
|
||||
input_file = 'input.csv'
|
||||
output_file = 'output.csv'
|
||||
|
||||
print(f"读取输入文件:{input_file}")
|
||||
points = read_input_csv(input_file)
|
||||
print(f"读取到 {len(points)} 个点")
|
||||
|
||||
if not points:
|
||||
print("没有数据,退出")
|
||||
return
|
||||
|
||||
# 找到最小刻度
|
||||
min_x_size, min_y_size = find_min_sizes(points)
|
||||
print(f"最小刻度:X={min_x_size}, Y={min_y_size}")
|
||||
|
||||
# 计算网格坐标
|
||||
points = calculate_grid_coords(points, min_x_size, min_y_size)
|
||||
|
||||
# 构建网格
|
||||
grid, width, height = build_grid(points)
|
||||
print(f"网格尺寸:{width} x {height}")
|
||||
|
||||
# 输出结果
|
||||
write_output_csv(output_file, grid)
|
||||
print(f"输出文件已保存:{output_file}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
58
README.md
Normal file
58
README.md
Normal file
@@ -0,0 +1,58 @@
|
||||
# ListToMAP
|
||||
|
||||
将 CSV 中的点数据转换为二维网格分布的 CSV 表格。
|
||||
|
||||
## 功能说明
|
||||
|
||||
1. **输入格式**:CSV 文件,包含 6 列
|
||||
- A 列:序号
|
||||
- B 列:名称
|
||||
- C 列:X 位置
|
||||
- D 列:Y 位置
|
||||
- E 列:X 大小
|
||||
- F 列:Y 大小
|
||||
|
||||
2. **处理逻辑**:
|
||||
- 以最小的 X 大小/Y 大小 为刻度单位
|
||||
- 根据 X/Y 位置除以刻度后四舍五入,确定网格坐标
|
||||
- 左下角为坐标原点
|
||||
|
||||
3. **输出格式**:
|
||||
- 每个单元格内容:`序号 + 换行 + 名称`
|
||||
- 输出为新的 CSV 文件
|
||||
|
||||
## 项目结构
|
||||
|
||||
```
|
||||
ListToMAP/
|
||||
├── Code/ # 代码文件夹
|
||||
│ ├── src/ # 源代码
|
||||
│ │ └── main.py # 主程序
|
||||
│ ├── config/ # 配置文件
|
||||
│ └── docs/ # 文档
|
||||
│ └── README.md # 详细文档
|
||||
├── Releases/ # 发布包文件夹
|
||||
│ └── v1.0/ # v1.0 版本
|
||||
├── Test/ # 测试文件夹
|
||||
│ ├── test_code/ # 测试代码
|
||||
│ ├── test_data/ # 测试数据
|
||||
│ │ └── input.csv # 示例输入
|
||||
│ ├── reports/ # 测试报告
|
||||
│ └── logs/ # 测试日志
|
||||
└── README.md # 本文件
|
||||
```
|
||||
|
||||
## 使用方法
|
||||
|
||||
```bash
|
||||
cd Code/src
|
||||
python3 main.py
|
||||
```
|
||||
|
||||
## 依赖
|
||||
|
||||
仅使用 Python 标准库(csv 模块),无需安装任何第三方库。
|
||||
|
||||
## 版本
|
||||
|
||||
- v1.0 - 初始版本
|
||||
0
Test/logs/.gitkeep
Normal file
0
Test/logs/.gitkeep
Normal file
0
Test/reports/.gitkeep
Normal file
0
Test/reports/.gitkeep
Normal file
9
Test/test_data/input.csv
Normal file
9
Test/test_data/input.csv
Normal file
@@ -0,0 +1,9 @@
|
||||
1,点 A,0,0,10,10
|
||||
2,点 B,10,0,10,10
|
||||
3,点 C,20,0,10,10
|
||||
4,点 D,0,10,10,10
|
||||
5,点 E,10,10,10,10
|
||||
6,点 F,20,10,10,10
|
||||
7,点 G,0,20,10,10
|
||||
8,点 H,10,20,10,10
|
||||
9,点 I,20,20,10,10
|
||||
|
Reference in New Issue
Block a user