Wolfram ResearchProductsPurchasingServices & ResourcesAbout UsOur Sites
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.

Documentation / Mathematica / Mathematicaブック / Mathematicaの仕組み / ファイルとストリーム /

2.12.7 ファイルのインポートとエキスポート

リストとデータの表のインポートおよびエキスポート

データリストをファイルout1.datにエキスポートする.

In[1]:= Export["out1.dat", {6.7, 8.2, -5.3}, "List"]

Out[1]=

ファイルの内容を示す.

In[2]:= !!out1.dat

Out[2]=

Mathematicaに内容を再びインポートする.

In[3]:= Import["out1.dat", "List"]

Out[3]=

データをMathematica内だけで使用する場合は,  2.12.1に示されているように,構造が保存されている完全なMathematicaの式として,ファイルにそのまま保存しておくのが一番よい.しかし他のプログラムとデータを交換したい場合は簡単なリストやテーブル形式で保存するほうがより便利である.

2次元のデータの配列のエキスポート.

In[4]:= Export["out2.dat", {{5.6 10^12, 7.2 10^12}, {3, 5}}, "Table"]

Out[4]=

必要であれば,CまたはFortran形式の「E」記号で数値が書き込まれる.

In[5]:= !!out2.dat

"5.6e12 7.2e12

3 5"

Mathematicaに配列を再びインポートする.

In[6]:= Import["out2.dat", "Table"]

Out[6]=

各行に1個の数値だけがあるファイルではImport["file", "List"]により,数値のリストとしてファイルの内容をインポートすることができる.各行がタブやスペースによって区切られた複数の数値からなる場合は,Import["file", "Table"]で数値のリストが得られる.ファイルに非数値の項目が含まれる場合は,Mathematicaの配列として返される.

文章データと数値データの混合をエキスポートする.

In[7]:= Export["out3.dat", {{"first", 3.4}, {"second", 7.8}}]

Out[7]=

エキスポートされたデータ.

In[8]:= !!out3.dat

"first 3.4

second 7.8"

データをMathematicaに再びインポートする.

In[9]:= Import["out3.dat", "Table"]

Out[9]=

InputFormを用いて,文字列を表示することができる.

In[10]:= InputForm[%]

Out[10]//InputForm= {{"first", 3.4}, {"second", 7.8}}

異なるフォーマットでファイルをインポートする

2行から成るファイルを作成する.

In[11]:= Export["out4.dat",
{"The first line.", "The second line."}, "Lines"]

Out[11]=

ファイルの内容を示す.

In[12]:= !!out4.dat

"The first line.

The second line."

ファイル全体を単一の文字列としてインポートする.

In[13]:= Import["out4.dat", "Text"]//InputForm

Out[13]//InputForm= "The first line.\nThe second line."

テキスト行のリストとしてファイルをインポートする.

In[14]:= Import["out4.dat", "Lines"]//InputForm

Out[14]//InputForm= {"The first line.", "The second line."}

空白で分離された語のリストとしてファイルをインポートする.

In[15]:= Import["out4.dat", "Words"]//InputForm

Out[15]//InputForm= {"The", "first", "line.", "The", "second", "line."}



Any questions about topics on this page? Click here to get an individual response.Buy NowMore Information


 © 2008 Wolfram Research, Inc.  Terms of Use  Privacy Policy |
Sign up for our newsletter: