C++调用python函数问题记录

OctoberKey / 2024-04-29 / 原文

问题1

# gm => excel
def export(gm, excel, select, unit, hide):
  ret = read_gm(gm)
  data = ret[0]
  channels = ret[1]
  msg = ret[2]
  if msg == 'ok':
    msg = write_excel(data, channels, excel, select, unit, hide)
  if msg == 'ok':
    os.system(f'"{excelfilepath}"')
  return msg

msg = export(gmfilepath, excelfilepath, exportselect, resistanceunit, hidecolumn)
print(msg)


分析:PyImport_ImportModule会调用最底下的代码,而export调用的传入参数未被初始化,故报错。

问题2

# gm => excel
def export(gm, excel, select, unit, hide):
  ret = read_gm(gm)
  data = ret[0]
  channels = ret[1]
  msg = ret[2]
  if msg == 'ok':
    msg = write_excel(data, channels, excel, select, unit, hide)
  if msg == 'ok':
    os.system(f'"{excelfilepath}"')
  return msg


分析:和问题1类似,excelfilepath未初始化。