如何在 Python 中使用 pandas 读取 KiCAD 贴片位置文件

如果你使用 GUI 或命令行导出了 KiCAD 贴片位置文件

kicad_export_pos_to_file.sh
kicad-cli pcb export pos MyPCB.kicad_pcb --units mm -o MyPCB.pos

你可以在 Python 脚本中使用 pandas.read_table(...) 这样读取它:

read_pos_with_pandas.py
import pandas as pd

pos = pd.read_table('KKS-Microcontroller-Board-R2.2.pos', delim_whitespace=True, names=["Ref", "Val", "Package", "PosX", "PosY", "Rot","Side"], comment="#")

可选地,你也可以通过 Ref 列(包含如 C12D1U5 等值)索引 pos

set_index_example.py
pos.set_index("Ref", inplace=True)

你还可以将所有这些打包到一个函数中:

read_kicad_pos_file.py
def read_kicad_pos_file(filename):
    pos = pd.read_table(filename, delim_whitespace=True, names=["Ref", "Val", "Package", "PosX", "PosY", "Rot","Side"], comment="#")
    pos.set_index("Ref", inplace=True)
    return pos

如果你使用了 .set_index(),你可以使用以下方式访问如 C13 的组件

example_lookup.txt
pos.loc["C13"]

示例输出:

example_output.txt
Val                100nF_25V
Package    C_0603_1608Metric
PosX                187.1472
PosY               -101.8243
Rot                    180.0
Side                     top
Name: C1, dtype: object

Check out similar posts by category: KiCad, Pandas, Python