Netlogo 文件打印在ask中似乎不起作用
我不知道文件打印在ask中不起作用,但在ask之外肯定起作用。有人能帮帮我吗,我做错了什么?对我来说很好:Netlogo 文件打印在ask中似乎不起作用,netlogo,Netlogo,我不知道文件打印在ask中不起作用,但在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"
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
顺便说一下,您应该使用而不是在不添加换行符的情况下写入文件
文件写入会做一些奇怪的事情,比如在所有字符串周围加引号,这就是为什么上面的输出中逗号是引号
更好的是,您应该使用来连接字符串,而不是一次又一次的文件写入或文件类型。我会这样写上面的代码:
"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
...
这将产生:
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
这可能是你想要的。顺便说一句,在CSV中,字符串中可以有空格,而不在其周围加引号。唯一需要引号的时候是字符串本身包含逗号的时候
最后,请注意,如果该文件已经存在,则只会将其追加到文件中,而不会覆盖。所以,您可能需要一些类似于文件是否存在的信息?文件打开前的文件名[文件删除文件名]。对我来说很好:
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
顺便说一下,您应该使用而不是在不添加换行符的情况下写入文件
文件写入会做一些奇怪的事情,比如在所有字符串周围加引号,这就是为什么上面的输出中逗号是引号
更好的是,您应该使用来连接字符串,而不是一次又一次的文件写入或文件类型。我会这样写上面的代码:
"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
...
这将产生:
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
这可能是你想要的。顺便说一句,在CSV中,字符串中可以有空格,而不在其周围加引号。唯一需要引号的时候是字符串本身包含逗号的时候
最后,请注意,如果该文件已经存在,则只会将其追加到文件中,而不会覆盖。所以,您可能需要一些类似于文件是否存在的信息?文件打开前的文件名[文件删除文件名]。请改用文件类型。如果确实需要在字符串周围加引号,可以添加引号
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
...
改用文件类型。如果确实需要在字符串周围加引号,可以添加引号
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
...
代码在我看来是正确的。你有什么证据证明它不起作用?生成的文件是什么样子的?我的编辑器在Windows中出现了问题。啊,重复的then@SethTisue是的,你说得对。后来我注意到了。但是,这些答案也纠正了我使用文件写入错误地写入文件的错误。我想这也应该对其他人有所帮助。我觉得代码看起来是正确的。你有什么证据证明它不起作用?生成的文件是什么样子的?我的编辑器在Windows中出现了问题。啊,重复的then@SethTisue是的,你说得对。后来我注意到了。但是,这些答案也纠正了我使用文件写入错误地写入文件的错误。我想这对其他人也有帮助。太好了!谢谢你的详细回答。我在Windows中的编辑器在我询问的问题上出现问题。你的其他建议也很有帮助!再次感谢,太好了!谢谢你的详细回答。我在Windows中的编辑器在我询问的问题上出现问题。你的其他建议也很有帮助!再次感谢。