如何使用netlogo读取.CSV文件? [英] How to read a .CSV file with netlogo?
问题描述
我现在开始在netlogo,所以我有一些问题,我没有找到如何解决他们。
我必须读取一个巨大的.csv文件,网上有这个代码:
I'm starting in netlogo these days, so I've got some problems that I didn't find how to solve them. I have to read a huge .csv file, got this code on the web:
to openFile
file-open "testeCsv.csv"
set csv file-read-line
set csv word csv "," ; add comma for loop termination
let mylist [] ; list of values
while [not empty? csv]
[
let $x position "," csv
let $item substring csv 0 $x ; extract item
carefully [set $item read-from-string $item][] ; convert if number
set mylist lput $item mylist ; append to list
set csv substring csv ($x + 1) length csv ; remove item and comma
set fileList mylist
]
set fileList mylist
show fileList
end
此文件包含此行:1; 0; 0; 65; 0; 2; 45; 0; -0,018961934
此代码的输出is:1 18961934
帮助:/
This file contains this line: "1;0;0;65;0;2;45;0;-0,018961934" The output of this code is: "1 18961934" Help :/
推荐答案
代码看起来基本正确。我假设csv和fileList被定义为全局变量,否则每个的设置命令将生成编译时错误。我们还可以在和
块中删除对fileList的引用,因为一旦而
循环退出时,它将被重置。然后,如果你想保留分号作为分隔符,我们可以做出这样的改变。总之,我们得到:
The code seems basically correct. I assume that csv and fileList are defined as global variables, otherwise the "set" commands on each will generate compile-time errors. We can also delete the reference to fileList in the while
block since it is reset anyway once the while
loop exits. Then, if you want to keep the semicolon as the separator we can make that change as well. Altogether, we get:
globals [csv fileList]
to openFile
file-open "testeCsv.csv"
set csv file-read-line
set csv word csv ";" ; add semicolon for loop termination
let mylist [] ; list of values
while [not empty? csv]
[
let $x position ";" csv
let $item substring csv 0 $x ; extract item
carefully [set $item read-from-string $item][] ; convert if number
set mylist lput $item mylist ; append to list
set csv substring csv ($x + 1) length csv ; remove item and comma
]
set fileList mylist
show fileList
end
b $ b
当我在你提供的CSV文件(在将文件中的小数分隔符从,转换为。之后)上运行时,我得到你寻找的结果,[1 0 0 65 0 2 45 0 -0.018961934]。
When I run this on the line in the CSV file you provide (after converting the decimal separator in the file from "," to ".") I get the result you seek, [1 0 0 65 0 2 45 0 -0.018961934].
当然,只读取文件中的一行。我假设你将要循环遍历文件中的每一行,存储或使用每行,因为它被读取。以下将每行存储在列表列表中。
Of course that reads only one line in the file. I assume that you will want to loop through each line in the file, storing or using each line as it is read. The following stores each line in a list of lists.
globals[csv fileList]
to openFile
file-open "testeCsv.csv"
set fileList []
while [not file-at-end?] [
set csv file-read-line
set csv word csv ";" ; add comma for loop termination
let mylist [] ; list of values
while [not empty? csv]
[
let $x position ";" csv
let $item substring csv 0 $x ; extract item
carefully [set $item read-from-string $item][] ; convert if number
set mylist lput $item mylist ; append to list
set csv substring csv ($x + 1) length csv ; remove item and comma
]
set fileList lput mylist fileList
]
show fileList
file-close
end
希望这有帮助。
Charles
Hope this helps. Charles
这篇关于如何使用netlogo读取.CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!