文件打印似乎在询问中不起作用
file-print doesn't seem to work in ask
breed[inboxturtles inboxturtle]
to check
create-inboxturtles 100[setxy random-xcor random-ycor]
file-open "D:\hello.csv"
file-write "X Coordinate" file-write "," file-write "Y Coordinate" file-write ","
file-write "Who" file-print ""
ask inboxturtles [
file-print ""
file-write xcor file-write ","
file-write ycor file-write ","
file-write who
]
file-close
end
我不知道 file-print
在 ask
中不起作用,但它肯定在 ask
之外起作用。谁能帮帮我,我做错了什么?
适合我:
"X Coordinate" "," "Y Coordinate" "," "Who"
-11.273903984790302 "," 11.589865065737627 "," 60
-3.198704310517442 "," -2.327808927515365 "," 6
13.485197306065764 "," -3.747989432973762 "," 91
16.085782333733263 "," 13.530031781112555 "," 35
...
顺便说一下,您应该使用 file-type
instead of file-write
写入文件而不附加换行符。
file-write
做了奇怪的事情,比如在所有字符串周围加上引号(这就是为什么逗号在上面的输出中被引号)。
更好的是,您应该使用 word
来连接字符串,而不是一遍又一遍地使用 file-write
(或 file-type
)。我会这样写上面的代码:
to check
create-inboxturtles 100[setxy random-xcor random-ycor]
file-open "D:\hello.csv"
file-print "X Coordinate,Y Coordinate,Who"
ask inboxturtles [
file-print (word xcor "," ycor "," who)
]
file-close
end
这输出:
X Coordinate,Y Coordinate,Who
-5.409837709344972,-0.6301891295194455,165
15.024747417946124,-9.591123025568086,193
9.735972095912903,-3.935540025692582,176
-11.505336629875304,-12.082889705829679,103
-10.19902536584426,-14.86360155896942,85
-5.928287603043071,7.175770417278386,22
-10.538908046584938,-15.009427435006804,120
...
这可能更符合您的要求。顺便说一句,在 CSV 中,字符串中可以有空格而不用引号引起来。只有在字符串本身包含逗号时才需要引号。
最后,请注意,如果文件已经存在,它只会被追加而不是被覆盖。所以你可能想要 file-open
.
之前的 if file-exists? filename [ file-delete filename ]
改用file-type
。如果你真的想要在字符串周围加上引号,你可以添加它们。
breed[inboxturtles inboxturtle]
to check
file-open "c:/temp/temp.csv"
file-type xcor
file-type "," file-type ycor
file-type "," file-type who
file-print ""
file-close
end
to setup
ca
carefully [file-delete "C:/temp/temp.csv"] []
file-open "C:/temp/temp.csv"
file-type "\"X Coordinate\""
file-type "," file-type "\"Y Coordinate\""
file-type ","
file-type "\"Who\""
file-print ""
file-close
create-inboxturtles 100[setxy random-xcor random-ycor]
end
to go
ask inboxturtles [check]
end
breed[inboxturtles inboxturtle]
to check
create-inboxturtles 100[setxy random-xcor random-ycor]
file-open "D:\hello.csv"
file-write "X Coordinate" file-write "," file-write "Y Coordinate" file-write ","
file-write "Who" file-print ""
ask inboxturtles [
file-print ""
file-write xcor file-write ","
file-write ycor file-write ","
file-write who
]
file-close
end
我不知道 file-print
在 ask
中不起作用,但它肯定在 ask
之外起作用。谁能帮帮我,我做错了什么?
适合我:
"X Coordinate" "," "Y Coordinate" "," "Who"
-11.273903984790302 "," 11.589865065737627 "," 60
-3.198704310517442 "," -2.327808927515365 "," 6
13.485197306065764 "," -3.747989432973762 "," 91
16.085782333733263 "," 13.530031781112555 "," 35
...
顺便说一下,您应该使用 file-type
instead of file-write
写入文件而不附加换行符。
file-write
做了奇怪的事情,比如在所有字符串周围加上引号(这就是为什么逗号在上面的输出中被引号)。
更好的是,您应该使用 word
来连接字符串,而不是一遍又一遍地使用 file-write
(或 file-type
)。我会这样写上面的代码:
to check
create-inboxturtles 100[setxy random-xcor random-ycor]
file-open "D:\hello.csv"
file-print "X Coordinate,Y Coordinate,Who"
ask inboxturtles [
file-print (word xcor "," ycor "," who)
]
file-close
end
这输出:
X Coordinate,Y Coordinate,Who
-5.409837709344972,-0.6301891295194455,165
15.024747417946124,-9.591123025568086,193
9.735972095912903,-3.935540025692582,176
-11.505336629875304,-12.082889705829679,103
-10.19902536584426,-14.86360155896942,85
-5.928287603043071,7.175770417278386,22
-10.538908046584938,-15.009427435006804,120
...
这可能更符合您的要求。顺便说一句,在 CSV 中,字符串中可以有空格而不用引号引起来。只有在字符串本身包含逗号时才需要引号。
最后,请注意,如果文件已经存在,它只会被追加而不是被覆盖。所以你可能想要 file-open
.
if file-exists? filename [ file-delete filename ]
改用file-type
。如果你真的想要在字符串周围加上引号,你可以添加它们。
breed[inboxturtles inboxturtle]
to check
file-open "c:/temp/temp.csv"
file-type xcor
file-type "," file-type ycor
file-type "," file-type who
file-print ""
file-close
end
to setup
ca
carefully [file-delete "C:/temp/temp.csv"] []
file-open "C:/temp/temp.csv"
file-type "\"X Coordinate\""
file-type "," file-type "\"Y Coordinate\""
file-type ","
file-type "\"Who\""
file-print ""
file-close
create-inboxturtles 100[setxy random-xcor random-ycor]
end
to go
ask inboxturtles [check]
end