This NetLogo extension adds CSV parsing capabilities to models.
このNetLogo拡張機能はCSV変換の機能をモデルに追加します。この機能を使うにはコードタブの冒頭に次のように書き加えます:
extensions [csv]
Just use csv:from-file "/path/to/myfile.csv"
! See from-file for more information.
単純にcsv:from-file "/path/to/myfile.csv"
を使います。詳細はfrom-fileをご覧ください。
For really big files, you may not want to store the entire file in memory, but rather just process it a line at a time. For instance, if you want to sum each of the columns of a numeric CSV file, you can do:
ファイルが大きい場合は、ファイルの全体はメモリには格納せずに、1回に1ラインのみを処理したいと考えるかもしれません。たとえば、数値CSVファイルの各列を合計したいときは、次のようにすることができます:
to-report sum-columns [ file ]
file-open file
set result csv:from-row file-read-line
while [ not file-at-end? ] [
let row csv:from-row file-read-line
set result (map [?1 + ?2] result row)
]
file-close
report result
end
You can also use this technique to...
このテクニックは次の場合にも使えます。
Here's an example model that reads in a file one line per tick:
以下は各ティックにファイルから1ラインを読み込むモデルの例です:
globals [ data ]
to setup
clear-all
file-close-all % Close any files open from last run
file-open "data.csv"
% other setup goes here他のセットアップをここで行う
reset-ticks
end
to go
if file-at-end? [ stop ]
set data csv:from-row file-read-line
% model update goes hereモデルの更新をここで行う
tick
end
Just use csv:to-file "/path/to/myfile.csv" my-data
! See to-file for more information.
単純にcsv:to-file "/path/to/myfile.csv" my-data
を使います。詳細はto-fileをご覧ください。
csv:from-row <string>
(csv:from-row <string> <delimiter>)
Parses the given string as though it were a row from a CSV file and returns it as a list of values. For example:
与えられた文字列をCSVファイルの1行のように扱い、値のリストとして返します。たとえば:
observer> show csv:csv-row-to-strings "one,two,three"
observer: ["one" "two" "three"]
Quotes can be used when items contain commas:
引用符を用いればカンマを含む要素を扱うことができます:
observer> show csv:csv-row-to-strings "there's,a,comma,\"in,here\""
observer: ["there's" "a" "comma" "in,here"]
You can put two quotes in a row to put an actual quote in an entry. If the entry is not quoted, you can just use one quote:
エントリーに引用符を含めたいときは2個の引用符を置きます。エントリーが引用符で囲われていないときは引用を1つだけ使うことができます:
observer> foreach (csv:from-row "he said \"hi there\",\"afterwards, she said \"\"hello\"\"\"") print
he said "hi there"
afterwards, she said "hello"
Number-like-entries will be parsed as numbers:
数値に相当するエントリーは数値に変換されます。
observer> show csv:from-row "1,-2.5,1e3"
observer: [1 -2.5 1000]
true
and false
with any capitalization will be parsed as booleans:
「true」と「false」は一部が大文字であっても論理変数に変換されます。
observer> show csv:from-row "true,TRUE,False,falsE"
observer: [true true false false]
To use a different delimiter, you can specify a second, optional argument. Only single character delimiters are supported:
カンマ以外のデリミタを使うにはオプションの第2引数で指定します。単一の文字のみがデリミタとして使えます。
observer> show (csv:csv-row-to-strings "1;2;3" ";")
observer: [1 2 3]
Different types of values can be mixed freely:
異なる種類の値であっても同時に扱うことができます。
observer> show csv:from-row "one,2,true"
observer: ["one" 2 true]
csv:from-string <string>
(csv:from-string <string> <delimiter>)
Parses a string representation of one or more CSV rows and returns it as a list of lists of values. For example:
1行もしくはそれ以上のCSV形式の文字列をリストのリストとして返します。たとえば:
observer> show csv:csv-to-strings "1,two,3\nfour,5,true"
observer: [[1 "two" 3] ["four" 5 true]]
csv:from-file <string>
(csv:from-file <string> <delimiter)
Parses an entire CSV file to a list of lists of values. For example, if we have a file example.csv
that contains:
CSVファイルの全体をリストのリストに変換します。たとえば、下記の内容を格納しているexample.csv
というファイルがあるとします:
1,2,3
4,5,6
7,8,9
10,11,12
Then, we get:
この場合、下記の出力を得ます。
observer> show csv:file-to-strings "example.csv"
observer: [[1 2 3] [4 5 6] [7 8 9] [10 11 12]]
The parser doesn't care if the rows have different numbers of items on them. The number of items in the rows list will always be <number of delimiters> + 1
, though blank lines are skipped. This makes handling files with headers quite easy. For instance, if we have header.csv
that contains:
各行の要素数が異なっていても問題ありません。行リストの要素数は常に<デリミタの数> + 1
となり、空白の列はスキップされます。これによりヘッダーを含んでいるファイルの扱いが容易になります。たとえば、下記の内容を格納しているheader.csv
というファイルがあるとします:
My Data
2/1/2015
Parameters:
start,stop,resolution,population,birth?
0,4,1,100,true
Data:
time,x,y
0,0,0
1,1,1
2,4,8
3,9,27
This gives:
この場合、下記の出力を得ます。
observer> foreach csv:file-to-strings "header.csv" show
observer: ["My Data"]
observer: ["2/1/2015"]
observer: ["Parameters:"]
observer: ["start" "stop" "resolution" "population" "birth?"]
observer: [0 4 1 100 true]
observer: ["Data:"]
observer: ["time" "x" "y"]
observer: [0 0 0]
observer: [1 1 1]
observer: [2 4 8]
observer: [3 9 27]
csv:to-row <list>
(csv:to-row <list> <delimiter>)
Reports the given list as a CSV row. For example:
指定のリストをCSV行として返します。たとえば:
observer> show csv:to-row ["one" 2 true]
observer: "one,2,true"
csv:to-string <list>
(csv:to-string <list> <delimiter>)
Reports the given list of lists as a CSV string. For example:
指定のリストのリストをCSV文字列として返します。たとえば:
observer> show csv:to-string [[1 "two" 3] [4 5]]
observer: "1,two,3\n4,5"
csv:to-file <string> <list>
(csv:to-file <string> <list> <delimiter>)
Writes the given list of lists to a new CSV file. For example:
指定のリストのリストを新しいCSVファイルに書き込みます。たとえば:
observer> csv:to-file "myfile.csv" [[1 "two" 3] [4 5]]
will result in a file myfile.csv
containing:
とすれば、myfile.csv
の内容はこうなります:
1,two,3
4,5