こんにちはゲストさん。会員登録(無料)して質問・回答してみよう!

解決済みの質問

PHPで複雑なCSVを読み込みたい

大証が毎日提供している日経先物のcsvファイルを読み込んで、データベース化したいなと思っています。

下記のようなcsvです。

--------------------------------------------
"日経225先物取引"
"限月","取引最終日","コード","約定指数",,,,,"前日比較","最終気配","取引高概算","清算指数","建玉残高概算"
,,,,"始値","高値","安値","終値"
,,,,"円","円","円","円","円","円","単位","円","単位"
"19年 6月"," 6. 7","162060018"," ","… ","… ","… ","… ","…"," …","… ","17912.59","230446"
" "," ","(SP)"," "," "," "," "," ","  ","  ","(…)"," "," "
"19年 9月"," 9.13","162090018"," ","17880 ","17890 ","17720 ","17790 ","-310"," …","135069 ","17790.00","270392"
" "," ","(SP)"," "," "," "," "," ","  ","  ","(…)"," "," "
" "," ","(立会外)"," ","(18057)","(18057)","(17750)","(17807)","  ","  ","(11843)"," "," "
"19年 12月","12.13","162120018"," ","17870 ","17870 ","17710 ","17770 ","-270"," …","315 ","17770.00","5903"
" "," ","(SP)"," "," "," "," "," ","  ","  ","(…)"," "," "
"20年 3月"," 3.13","163030018"," ","17840 ","17860 ","17760 ","17790 ","-290"," …","9 ","17786.88","3931"
" "," ","(SP)"," "," "," "," "," ","  ","  ","(…)"," "," "
"20年 6月"," 6.12","163060018"," ","17850 ","17850 ","17700 ","17740 ","-300"," …","28 ","17735.58","1322"
" "," ","(SP)"," "," "," "," "," ","  ","  ","(…)"," "," "
"20年 9月"," 9.11","163090018"," ","17840 ","17840 ","17840 ","17840 ","…"," …","1 ","17760.52","1"
" "," ","(SP)"," "," "," "," "," ","  ","  ","(…)"," "," "
"平均株価"," ","日経225","","17904.68","17904.68","17696.51","17779.09","-274.29"," …","… ","…","…"
---------------------------------------------------


かなりごちゃごちゃですが、このようなcsvです。
例えばこのcsvから19年6月のデータだけ取得したい場合、
どのようなソースを書けばよいのでしょうか。

自分なりに考えてみたのですが、
19年6月は3行あります。SPとか立会外は不要ですので、つまり
"19年 9月"," 9.13","162090018"," ","17880 ","17890 ","17720 ","17790 ","-310"," …","135069 ","17790.00","270392"
この部分のみ必要です。

まずcsvを読み込み"19年 9月"を検索し、該当の行だけを抜くような形かな?と思っています。


よろしくお願いします。

投稿日時 - 2007-06-10 13:40:43

QNo.3072388

すぐに回答ほしいです

質問者が選んだベストアンサー

19年6月が欲しいのか、19年9月が欲しいのかわかりません。
問題提起の時点でブレがおおきいと、有効な回答は期待できません。

また、(SP)のデータとか(立会外)のデータとかには日付け情報が
のっていないようですが、これは指定がない場合は前の行のデータを
引き継ぐのでしょうか?仕様提起しっかりしていないと判断できません。

それとデータを取り出してどのように出力したいかもかいたほうが
よいですね。

総じて当座、こんな感じでいかがですか?

<?PHP

$handle = fopen("test.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
if(preg_match('/19年 6月/',$data[0])){
print_r($data);
}
}
fclose($handle);
?>

preg_match()ではなく単にデータを比較してもいいのですが、
すこし複雑な検索になると正規表現の方がやりやすいのであえてこうしました。

投稿日時 - 2007-06-10 14:43:05

補足

大変失礼致しました。

質問の19年6月はすべて19年9月と読み替えてください。


>また、(SP)のデータとか(立会外)のデータとかには日付け情報が
>のっていないようですが、これは指定がない場合は前の行のデータを
>引き継ぐのでしょうか?

その通りです。

データを取り出して、MySQLで管理したいと思っております。



サンプルスクリプトまで頂いてとても感謝しています。
今から実験してみます。
ありがとうございました。

投稿日時 - 2007-06-10 14:51:16

お礼

やりたいことが全て実現できました。

ありがとうございました。

投稿日時 - 2007-06-10 21:12:34

ANo.1

このQ&Aは役に立ちましたか?

0人が「このQ&Aが役に立った」と投票しています

回答(1)

あなたにオススメの質問