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

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

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

问题

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

我使用了以下代码:

import gdx
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:

import gdx
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

尝试以下代码:

from gdxpds import GdxFile

# 用实际的GDX文件路径替换 'path/to/your/resultados.gdx'
file_path = 'path/to/your/resultados.gdx'

# 需要打开GDX文件
gdx_file = GdxFile(file_path)

# 然后尝试获取GDX文件中的符号(表)列表
symbols = gdx_file.get_symbols()

# 创建一个字典来存储数据框
dataframes = {}

# 将每个符号转换为数据框并存储在字典中
for symbol in symbols:
    dataframes[symbol] = gdx_file.to_dataframe(symbol)

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

英文:

Try the following :

from gdxpds import GdxFile

# Replace 'path/to/your/resultados.gdx' with the actual path to your GDX file.
file_path = 'path/to/your/resultados.gdx'

# You need to open the GDX file
gdx_file = GdxFile(file_path)

# Then try to get the list of symbols (tables) in the GDX file
symbols = gdx_file.get_symbols()

# Create a dictionary to store the DataFrames
dataframes = {}

# Convert each symbol to a DataFrame and store it in the dictionary
for symbol in symbols:
    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:

确定