如何在Python中从GAMS中读取具有多个表的GDX文件?

huangapple go评论102阅读模式
英文:

how to read gdx from gams with multiple tables in python?

问题

我有一个包含多个表格的gdx文件,我需要将它们转换成Python中的数据框,但我甚至无法读取这个gdx文件。

我使用了以下代码:

  1. import gdx
  2. f = gdx.File('resultados.gdx', lazy=True, implicit=True, skip={})

我遇到了这个错误:

TypeError: 期望的是字符串、字节或os.PathLike对象,而不是NoneType。

英文:

I have a gdx with several tables and I need to convert them into dataframes in python but I can't even read the gdx.

I used:

  1. import gdx
  2. f = gdx.File('resultados.gdx', lazy=True, implicit=True, skip={})

I get this error:

>TypeError: expected str, bytes or os.PathLike object, not NoneType

答案1

得分: 0

尝试以下代码:

  1. from gdxpds import GdxFile
  2. # 用实际的GDX文件路径替换 'path/to/your/resultados.gdx'
  3. file_path = 'path/to/your/resultados.gdx'
  4. # 需要打开GDX文件
  5. gdx_file = GdxFile(file_path)
  6. # 然后尝试获取GDX文件中的符号(表)列表
  7. symbols = gdx_file.get_symbols()
  8. # 创建一个字典来存储数据框
  9. dataframes = {}
  10. # 将每个符号转换为数据框并存储在字典中
  11. for symbol in symbols:
  12. dataframes[symbol] = gdx_file.to_dataframe(symbol)

这是您提供的代码的翻译部分。

英文:

Try the following :

  1. from gdxpds import GdxFile
  2. # Replace 'path/to/your/resultados.gdx' with the actual path to your GDX file.
  3. file_path = 'path/to/your/resultados.gdx'
  4. # You need to open the GDX file
  5. gdx_file = GdxFile(file_path)
  6. # Then try to get the list of symbols (tables) in the GDX file
  7. symbols = gdx_file.get_symbols()
  8. # Create a dictionary to store the DataFrames
  9. dataframes = {}
  10. # Convert each symbol to a DataFrame and store it in the dictionary
  11. for symbol in symbols:
  12. dataframes[symbol] = gdx_file.to_dataframe(symbol)

huangapple
  • 本文由 发表于 2023年7月20日 15:45:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定