Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
6.1.1 - 日本語

Change language to:
English - Français - Português - Русский

Please note that the recommended version of Scilab is 2024.0.0. This page might be outdated.
See the recommended documentation of this function

Scilabヘルプ >> Spreadsheet > csvRead

csvRead

CSVファイルを読み込む

呼び出し手順

M = csvRead(filename)
M = csvRead(filename, separator)
M = csvRead(filename, separator, decimal)
M = csvRead(filename, separator, decimal, conversion)
M = csvRead(filename, separator, decimal, conversion, substitute)
M = csvRead(filename, separator, decimal, conversion, substitute, regexpcomments, range)
M = csvRead(filename, separator, decimal, conversion, substitute, regexpcomments, range, header)
[M, comments] = csvRead(filename, separator, decimal, conversion, substitute, regexpcomments, range, header)

引数

filename

1行1列の文字列行列で,ファイルパスです.

separator

1行1列の文字列行列で, 使用されるフィールドセパレータ.

decimal

1行1列の文字列行列で,使用される小数点.

decimal[] と異なり conversionstringに 設定された場合,小数点変換が行われます.

conversion

1行1列の文字列行列で,出力Mの型. 利用可能な値は "string" または "double" (デフォルト)です.

read_csv では "string" がデフォルトであることに 注意してください.

substitute

m行2列の文字列行列で, 置換マップ(デフォルト = [], 置換処理なし). 最初の列 substitute(:,1) は 検索する文字列を有し, 2番目の列 substitute(:,2) は置換文字列を 有します. ファイル中に指定された文字列が現れる度に 置換が行われます.

rexgepcomments

a string specifying a regular expression (default: []). Lines of the file matching the expression are considered as comments.

The same character is expected as delimiter at the beginning and at the end of the string. If it is needed in the expression's body, it must be escaped with "\" (Here is a good online page explaining the syntax of regular expressions).

range

[firstRow firstColumn lastRow lastColumn] row vector of floating point integers, with lastRow ≤ 2e9 and lastColumn ≤ 2e9: the range of rows and columns selecting the block of data to be returned. Default [], meaning all data. The block is selected on actual data, after removing the header and/or commented rows (when requested).

To select all rows or/and columns starting from [firstRow, firstCol], while the total numbers of rows or columns are unknown, lastRow=2e9 or/and lastColumn=2e9 can be specified (the actual limit is 231).
header

1行1列の行列の浮動小数点整数, ファイルの先頭で無視される行の数.

M

m行n列の文字列またはdouble行列. Complex numbers are supported.

comments

a column vector of strings: Lines of text matching regexpcomments.

説明

カンマ区切りの値をフィールドとするアスキーファイルを指定すると, この関数は対応する文字列またはdoubleのScilab行列を返します.

例えば,.csvデータファイルは 表計算ソフトウェアにより"テキスト及びカンマ"形式で 作成されたものとすることができます.

列はカンマ以外で区切られたものとすることもできます. この場合, 別のセパレータを指定する csvRead(filename, separator) を使用してください.

オプションの入力引数のデフォルト値は, csvDefault 関数で定義されます.

オプションの入力引数に空の行列[]を指定すると, デフォルト値が設定されます.

入力引数 "conversion" が "double" の時, .csvの中の数値以外のフィールド(例:文字列)は NaNに変換されます.

csvRead は UTF-8 および ASCII テキストファイルを読み込むことができます.

以下のスクリプトは, csvRead関数のいくつかの基本的な使用例を示します.

// タブ区切りのデータを含むファイルを生成
M = 1:50;
filename = fullfile(TMPDIR, "data.csv");
csvWrite(M, filename, ascii(9), '.');
// csvファイルを読み込む
M1 = csvRead(filename,ascii(9), [], 'string')
// doubleを返す
M2 = csvRead(filename,ascii(9), '.', 'double')
// 元のデータを結果と比較
and(M == M2)
// 特殊なデータファイルを管理するために
// 置換用引数を使用
content = [
"1"
"Not-A-Number"
"2"
"Not-A-Number"
];
substitute = [
"Not-A-Number" "Nan"
];
mputl(content,filename);
M = csvRead(filename,",",".","double",substitute)
isnan(M(2,1)) // Expected=%t
isnan(M(4,1)) // Expected=%t

以下のスクリプトは, csvRead関数のより実用的な使用例を示します.

// 文字列の行列を定義
Astr = [
"1" "8" "15" "22" "29" "36" "43" "50"
"2" "9" "16" "23" "30" "37" "44" "51"
"3" "10" "17" "6+3*I" "31" "38" "45" "52"
"4" "11" "18" "25" "32" "39" "46" "53"
"5" "12" "19" "26" "33" "40" "47" "54"
"6" "13" "20" "27" "34" "41" "48" "55"
"+0" "-0" "Inf" "-Inf" "Nan" "1.D+308" "1.e-308" "1.e-323"
];
// カンマ区切りのデータを有するファイルを作成
filename = fullfile(TMPDIR , 'foo.csv');
sep = ",";
fd = mopen(filename,'wt');
for i = 1 : size(Astr,"r")
        mfprintf(fd,"%s\n",strcat(Astr(i,:),sep));
end
mclose(fd);
// ファイルを参照するには: edit(filename)
// このファイルを読み込む
Bstr = csvRead ( filename )
// 特殊なセパレータを有するファイルを作成: ここでは ";"
filename = fullfile(TMPDIR , 'foo.csv');
sep = ";";
fd = mopen(filename,'wt');
for i = 1 : size(Astr,"r")
        mfprintf(fd,"%s\n",strcat(Astr(i,:),sep));
end
mclose(fd);
//
// セパレータを指定してファイルを読み込む
csvRead ( filename , sep )

以下のスクリプトは csvRead 関数のregexp引数により 行を削除する例を示します.

CSV = ["// tata"; ..
"1,0,0,0"; ..
"// titi"; ..
"0,1,0,0"; ..
" //  toto"; ..
"0,0,1,0"; ..
" tutu // tata"];
filename = fullfile(TMPDIR , 'foo.csv');
mputl(CSV, filename);

// Ignore all lines including "//" and return them as comments:
[M, comments] = csvRead(filename, [], [], [], [], '!//!')
--> [M, comments] = csvRead(filename, [], [], [], [], '!//!')
 M  =
   1.   0.   0.   0.
   0.   1.   0.   0.
   0.   0.   1.   0.

 comments  =
  "// tata"
  "// titi"
  " //  toto"
  " tutu // tata"

空のフィールドはcsvReadで管理されます

csvWrite(['1','','3';'','','6'], TMPDIR + "/example.csv")
csvRead(TMPDIR + "/example.csv", [], [], "string")
csvRead(TMPDIR + "/example.csv", [], [], "double")

// 文字列の行列を定義
Astr = [
"1" "8" "15" "22" "29" "36" "43" "50"
"2" "9" "16" "23" "30" "37" "44" "51"
"3" "10" "17" "6+3*I" "31" "38" "45" "52"
"4" "11" "18" "25" "32" "39" "46" "53"
"5" "12" "19" "26" "33" "40" "47" "54"
"6" "13" "20" "27" "34" "41" "48" "55"
"+0" "-0" "Inf" "-Inf" "Nan" "1.D+308" "1.e-308" "1.e-323"
];
// カンマ区切りのデータを有するファイルを作成
filename = fullfile(TMPDIR , 'foo.csv');
sep = ",";
fd = mopen(filename,'wt');
for i = 1 : size(Astr,"r")
        mfprintf(fd,"%s\n",strcat(Astr(i,:),sep));
end
mclose(fd);
// ファイルを参照するには: edit(filename)
// このファイルを読み込む
Bstr = csvRead ( filename )
// 特殊なセパレータを有するファイルを作成: ここでは ";"
filename = fullfile(TMPDIR , 'foo.csv');
sep = ";";
fd = mopen(filename,'wt');
for i = 1 : size(Astr,"r")
        mfprintf(fd,"%s\n",strcat(Astr(i,:),sep));
end
mclose(fd);
//
// セパレータを指定してファイルを読み込む
csvRead ( filename , sep )

以下のスクリプトでは, ファイル "filename" は 5000行単位のブロックで読みこまれます. 実際に読み込まれたファイルの行数が5000行に 満たない時, すなわち, ファイルの終端に達した時, このアルゴリズムは中断します.

blocksize = 5000;
C1 = 1;
C2 = 3;
iblock = 1
while %t
    R1 = (iblock-1) * blocksize + 1;
    R2 = blocksize + R1-1;
    irange = [R1 C1 R2 C2];
    mprintf("\nBlock #%d, rows #%d to #%d\n",iblock,R1,R2);
    tic();
    M=csvRead(filename , [] , [] , [] , [] , [] , irange );
    t = toc();
    nrows = size(M,"r");
    ncols = size(M,"c");
    if ( nrows > 0 ) then
        p = t/(nrows*ncols)*1.e6;
        mprintf("  Actual #rows=%d\n",nrows);
        mprintf("  T=%.3f (s)\n",t);
        mprintf("  T=%.1f (ms/cell)\n",p);
    end
    if ( nrows < blocksize ) then
        mprintf("... End of the file.\n");
        break
    end
    iblock = iblock + 1;
end

出力は以下のようになります :

Block #1, rows #1 to #5000
Actual #rows=5000
T = 3.135 (s)
T = 209.0 (ms/cell)

Block #2, rows #5001 to #10000
Actual #rows=5000
T = 3.139 (s)
T = 209.3 (ms/cell)

Block #3, rows #10001 to #15000
Actual #rows=5000
T = 3.151 (s)
T = 210.1 (ms/cell)

etc....

範囲を有する例

CSV = ["1,0,0,0,0"; ..
"0,1,0,0,0"; ..
"0,0,1,0,0"; ..
"4,4,1,2,0"; ..
"4,63,1,2,0"; ..
"4,63,1,4,233"; ..
"42,3,23,2,23"; ..
];
filename = fullfile(TMPDIR , 'foo.csv');
mputl(CSV, filename);
// csvファイルの一部を展開
csvRead(filename, [], [], "double", [], [], [5 3 7 6])

ヘッダを有する例

comments = [
"// Copyright (C) INRIA"];
filename = fullfile(TMPDIR , 'foo.csv');
csvWrite(rand(2,3), filename, ascii(9), ",", [], comments);
header = 2;
[M, c] = csvRead(filename, ascii(9), ",", "double", [], [], [], header) // 最初の2行(ヘッダ)を無視

参照

履歴

バージョン記述
5.4.0 関数が導入されました. 'csv_readwrite'モジュールに基づきます. read_csv と比べた場合の 動作の唯一の差異は, read_csvが値を文字列として返す時,デフォルトで値をdoubleに変換しようとする ことです.
5.4.1 decimal[] 以外で, conversionstringに設定された場合, 小数点変換が行われます.
5.5 ヘッダを無視するために,入力引数"header"が追加されました.
Report an issue
<< csvDefault Spreadsheet csvTextScan >>

Copyright (c) 2022-2023 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Mon Jan 03 14:38:06 CET 2022