网志导出Tableau问题

问题描述 投票:3回答:1

目前我正在玩从Netlogo导出数据到CSV文件,然后用以下代码加载到Tableau中。

to write-result-to-file
  ; if nothing to write then  stop
  if empty? Result-File [stop]
  ; Open file
  file-open Result-File

  ; Write into the file
  file-print (word Days-passed "," num-susceptible "," num-infected "," num-recovered)
  ; Close file
  file-close
end

我遇到的问题是,当我把数据加载到Tableau中时,它不能正确地拾取措施尺寸。有没有办法在Netlogo中指定我的每一个rowscolumns的头部,然后再导出到CSV文件?

csv netlogo tableau
1个回答
3
投票

这个问题在NetLogo用户上提出并回答。 James Steiner的回答复制如下,并纠正了代码中的一些错别字。 它真的很优雅。


在设置过程中,您可以将标题打印到结果文件中。

你可能想建立一个子程序来处理所有写到文件的工作,这样你就不用重复代码了。

to write-csv [ #filename #items ]
  ;; #items is a list of the data (or headers!) to write.
  if is-list? #items and not empty? #items
  [ file-open #filename
  ;; quote non-numeric items
  set #items map quote #items
  ;; print the items
  ;; if only one item, print it.
  ifelse length #items = 1 [ file-print first #items ]
  [file-print reduce [ (word ?1 "," ?2) ] #items]
  ;; close-up
  file-close
  ]
end

to-report quote [ #thing ]
  ifelse is-number? #thing
  [ report #thing ]
  [ report (word "\"" #thing "\"") ]
end

你可以用

write-csv "myfilename.csv" ["label1" "label2" "label3"]

在您的设置程序中写入列头,然后用

write-csv "myfilename.csv" [10.0 "sometext" 20.3]

写一行数据--本例中是一个数字、一个字符串和另一个数字。

© www.soinside.com 2019 - 2024. All rights reserved.