![](/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.