LoginSignup
1
0

More than 3 years have passed since last update.

今更CSVの形式について知る~RFC4180の2.Definition of the CSV Formatを読む~

Posted at

動機

CSVってよく使われてるけど、意外と何も知らないことに気づいてしまいました。
カンマで区切られていて、改行で1レコードが終わるくらいしか。。。
なので今更ではありますが、CSVの形式をちゃんと知ろうと思います。

形式について

では、RFC4180の 2.Definition of the CSV Format を読んでいきましょう。

各レコードはCRLFで区切る

1. Each record is located on a separate line, delimited by a line
break (CRLF). For example:

aaa,bbb,ccc CRLF
zzz,yyy,xxx CRLF

各レコードはCRLFで区切られますと。
改行ならなんでもいいのかと思ってましたが、CRLFでという決まりみたい。

ファイル最後のレコード末尾はCRLFあっても、なくてもよい

2. The last record in the file may or may not have an ending line
break. For example:

aaa,bbb,ccc CRLF
zzz,yyy,xxx

あってもよい(may)ってことなんで、zzz,yyy,xxx CRLF でおわってもいいってことですね。

ファイルの先頭にヘッダ行が存在してもよい

3. There maybe an optional header line appearing as the first line
of the file with the same format as normal record lines. This
header will contain names corresponding to the fields in the file
and should contain the same number of fields as the records in
the rest of the file (the presence or absence of the header line
should be indicated via the optional "header" parameter of this
MIME type). For example:

field_name,field_name,field_name CRLF
aaa,bbb,ccc CRLF
zzz,yyy,xxx CRLF

ちょっと長くて読むの嫌ですが、言ってることは大体下記です。

  • ファイルの先頭に、通常のレコード行と同じ構成のヘッダ行が存在してもよい
  • ヘッダには、レコードのフィールド名を含み、フィールド数はレコードのフィールド数と同じにすべき

あと

  • ヘッダ行の有無はMIMEタイプのオプションパラメータ"header"で示すべき

とも書いてありますね、()書きの部分です。
"header"パラメータについては、3. MIME Type Registration of text/csv に書いてあります。

The "header" parameter indicates the presence or absence of the header line. Valid values are "present" or "absent".

text/csv; header=present
ってなってたらヘッダ行がありますよ、っていうことになります。

ヘッダと各レコードはカンマ区切りで1つ以上のフィールドを含む。各行のフィールド数は同一にすべき。

4. Within the header and each record, there may be one or more
fields, separated by commas. Each line should contain the same
number of fields throughout the file. Spaces are considered part
of a field and should not be ignored. The last field in the
record must not be followed by a comma. For example:

aaa,bbb,ccc

またしてもちょっと長くて嫌ですね。言ってることはこんな感じです。

  • ヘッダと各レコードはカンマ区切りで1つ以上のフィールドを含む
  • ファイル全体を通して、各行のフィールド数は同一にすべき
  • スペースはフィールドの一部とみなし、無視すべきでない
  • レコードの最後のフィールドの末尾にカンマをつけてはいけない(must not)

各フィールドはダブルクォーテーションで囲っても、囲わなくてもよい

5. Each field may or may not be enclosed in double quotes (however
some programs, such as Microsoft Excel, do not use double quotes
at all). If fields are not enclosed with double quotes, then
double quotes may not appear inside the fields. For example:

"aaa","bbb","ccc" CRLF
zzz,yyy,xxx

また少し長め。言ってることは下記です。

  • 各フィールドはダブルクォーテーションで囲っても、囲わなくてもよい
  • ダブルクォーテーションで囲わない場合、フィールド中にダブルクォーテーションを含めてはいけない(may not)
OK
"aa""aaa",....

↑はOKだけど、

NG
aa""aaa,....

↑はNGってことですね。

余談ですけど、ダブルクォーテーションを口で言う時は、ダブルコート派です:raising_hand:

CRLF, ダブルクォーテーション, カンマを含むフィールドは、ダブルクォーテーションで囲むべき

6. Fields containing line breaks (CRLF), double quotes, and commas
should be enclosed in double-quotes. For example:

"aaa","b CRLF
bb","ccc" CRLF
zzz,yyy,xxx

CRLF, ダブルクォーテーション, カンマを含むフィールドは、ダブルクォーテーションで囲むべきですよと。
ここは特に言うことないですね。次で最後です。

ダブルクォーテーションをフィールド内で使う場合、ダブルクォーテーションでエスケープしなければならない

7. If double-quotes are used to enclose fields, then a double-quote
appearing inside a field must be escaped by preceding it with
another double quote. For example:

"aaa","b""bb","ccc"

ダブルクォーテーションをフィールドを囲むのに使われている場合、
フィールド内のダブルクォーテーションは、ダブルクォーテーションでエスケープしなければならない、みたいです。

ダブルクォーテーションて言葉が繰り返しで出てきてわかりにくいですが、

OK
"b""bb",....

↑はOKだけど、

NG
"b"bb",....

↑はNGてことですね。

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0