![](/img/trans.png)
[英]I need to read in a text file with data into python and assign the data the variables
[英]Read variables from text file python (OPL Data file)
我有许多如下所示的文本 (.txt) 文件:
#camiones disponibles
set K:= 1 2 3;
#capacidades de camiones
param Q:=
1 20000
2 15000
3 10000
;
#demanda por tipo de leche
param D:=
26800
11700
2500
;
#costos de transporte
param c[*,*]
: 1000 1 2 3 4 5 6 7
1000 0 35 78 76 98 55 52 37
1 35 0 60 59 91 81 40 13
2 78 60 0 3 37 87 26 48
3 76 59 3 0 36 83 24 47
4 98 91 37 36 0 84 51 78
5 55 81 87 83 84 0 66 74
6 52 40 26 24 51 66 0 28
7 37 13 48 47 78 74 28 0
据我了解,这是一个 OPL 数据文件。 这些文本文件中的每一个都是一个实例,并且它们中的每一个都具有相同的变量。 我需要一次读取一个文本文件。
我正在尝试将每个变量定义放入 python 变量中,例如 numpy 数组或 pandas 数据框。
此数据文件不是 opl 格式,而是 ampl。 所以你可以做的是使用 ampl 读取文件,然后从 ampl 以你需要的任何格式写入数据
https://portal.ampl.com/docs/archive/first-website/BOOK/CHAPTERS/15-display.pdf#page=33
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.