NetLogo:将表格结果导出到CSV [英] NetLogo: Exporting table results into CSV

查看:45
本文介绍了NetLogo:将表格结果导出到CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该模型的目的是探索北落基山脉灰狼的潜在扩散模式。在该模型中,灰狼被赋予一个对应于空间数据表的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

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

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

有没有人推荐我如何有效地导出数据?

如有任何帮助,我们将不胜感激!

推荐答案

这里有两个问题。file-write不会在其输出的末尾放回车符,因此连续的file-write将所有内容字符串放在一个很长的行上。此外,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

勾选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

并且Excel可以正确打开它。

这篇关于NetLogo:将表格结果导出到CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆