英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论