NetLogo:将表结果导出到 CSV

Aly*_*a T 2 datatable excel export netlogo behaviorspace

该模型的目的是探索落基山脉北部灰狼的潜在扩散模式。在模型中,灰狼被赋予一个ph-memory与空间数据表相对应的属性。

extensions [ gis table csv]

wolves-own [
  ...
  ph-memory ;; wolves' patch-hunting memory, table includes the patch's hash-id (KEY) and pack-id (VALUE)
  ...   
]

to initialize-wolf  [  new-pack-id  ]
    ...
    set ph-memory     table:make
    ...
end

to setup
  clear-all
  setup-gis

  file-open (word "Ph-memory-for-run-" behaviorspace-run-number ".csv")
  ...
end

to go
  if not any? wolves [stop]
  ask wolves [    
    where-am-i
  ...
  file-write (table:to-list ph-memory)
end

to where-am-i
 let patch-hash-id ([hash-id] of patch-here)       ;;Updates the hash-id of the patch the wolf is currently on
    if not table:has-key? ph-memory patch-hash-id
    [table:put ph-memory patch-hash-id pack-id]             
end
Run Code Online (Sandbox Code Playgroud)

当我打开 Excel 文件查看结果时,整个表格将导出到单个单元格中。不幸的是,这使得数据分析变得毫无意义,因为我无法轻松地操纵数据。

excel输出

我的问题是:是否可以将数据表结果导出到 Excel 并将数据分解为单个单元格/离散数据对(例如 [ patch-hash-id, pack-id] )?我开始手动将数据重新格式化为列,但这非常乏味!

理想的结果

有人建议我如何有效地导出数据吗?

任何帮助将不胜感激!

Cha*_*les 5

这里有两个问题。 file-write不会在其输出末尾添加回车符,因此连续的file-writes 将所有内容串成一长行。此外,Excel 需要一个 CSV 文件,其中每行上的值均以逗号分隔,并table:to-list生成 id/值对列表的列表,但不使用逗号分隔值。CSV 扩展可以很好地做到这一点csv:to-string,并file-print提供回车符。下面的代码应该展示它们是如何组合在一起的。

extensions [table csv]
globals [ph-memory]

to setup
  clear-all
  set ph-memory table:from-list [[1 2] [3 4] [5 6]]
  reset-ticks
end

to go
  file-open "c:/users/cstaelin/desktop/testfile.csv"
  file-print csv:to-string table:to-list ph-memory
  file-close
end
Run Code Online (Sandbox Code Playgroud)

4 个刻度后,csv 文件看起来像

1,2
3,4
5,6
1,2
3,4
5,6
1,2
3,4
5,6
1,2
3,4
5,6
Run Code Online (Sandbox Code Playgroud)

Excel 可以正确打开它。