我有一个关于这个代码创建的乌龟数量的问题:
to read-turtles-from-csv
file-close-all ; close all open files
if not file-exists? "turtles.csv" [
user-message "No file 'turtles.csv' exists! Try pressing WRITE-TURTLES-TO-CSV."
stop
]
file-open "turtles.csv" ; open the file with the turtle data
; We'll read all the data in a single loop
while [ not file-at-end? ] [
let data csv:from-row file-read-line
create-turtles 1 [
print "item column 4"
show item 4 data
]
]
file-close ; make sure to close the file
end我的turtles.csv文件只有两行,所以我在这里期望的是对行数重复create-turts1,并且我有两个代理,其中第四列中的两个数字被打印出来。令人惊讶的是,4只乌龟被创造出来了!为什么?
谢谢
发布于 2017-06-27 23:26:58
我想知道你的turtles.csv是不是被读入的行数太多了?试着这样做:
to read-file
file-close-all
file-open "turtles.csv"
while [not file-at-end?] [
print csv:from-row file-read-line
]
file-close-all
end查看Netlogo是如何读取该文件的。看起来你是在正确的轨道上,否则--我只是按照你的例子测试了一个类似的文件,我得到了我的两只乌龟。使用名为"turtle_details.csv“的.csv文件,如下所示:
color size heading
1 red 2 90
2 blue 4 180我使用这段代码用.csv中的变量生成了两个海龟
extensions [ csv ]
to setup
ca
reset-ticks
file-close-all
file-open "turtle_details.csv"
;; To skip the header row in the while loop,
; read the header row here to move the cursor
; down to the next line.
let headings csv:from-row file-read-line
while [ not file-at-end? ] [
let data csv:from-row file-read-line
print data
create-turtles 1 [
set color read-from-string item 0 data
set size item 1 data
set heading item 2 data
]
]
file-close-all
endhttps://stackoverflow.com/questions/44771781
复制相似问题