此为 Mathematica 4 文档,内容基于更早版本的 Wolfram 语言
查看最新文档(版本11.2)

Import

Usage

Import["name.ext"] 从一个文件中输入数据,假设它的格式是由扩展文件名 ext 指定,并把它转换为一个Mathematica表达式。
Import["file", "format"]从一个文件中以指定的格式输入数据。


Notes

Import 试图给出一个Mathematica表达式,它的意义尽可能接近于外部文件的数据。
Import 文本和表格数据,还有图形和声音。 •对文本和表格数据支持下面的基本格式:
"\!\(\*StyleBox[\"\\\"\\\\\\\"Lines\\\\\\\"\\\"\", \"MR\"]\) "文本线
"\!\(\*StyleBox[\"\\\"\\\\\\\"List\\\\\\\"\\\"\", \"MR\"]\) "包含数字或字符串的线
"\!\(\*StyleBox[\"\\\"\\\\\\\"Table\\\\\\\"\\\"\", \"MR\"]\) "二维数字或字符串数组
"\!\(\*StyleBox[\"\\\"\\\\\\\"Text\\\\\\\"\\\"\", \"MR\"]\) "普通字符串
"\!\(\*StyleBox[\"\\\"\\\\\\\"UnicodeText\\\\\\\"\\\"\", \"MR\"]\) "16位Unicode字符串
"\!\(\*StyleBox[\"\\\"\\\\\\\"Words\\\\\\\"\\\"\", \"MR\"]\) "由空格或新行分离的字
"Text""UnicodeText"返回单一Mathematica串。
"Lines""Words"返回Mathematica串列表。
"List"返回Mathematica数和串列表。
"Table"返回Mathematica数和串列表的一个列表。
• 在 "List""Table" 格式中, 数可以在 C 或Fortran中象记号E一样被读入。
• 没有明确的十进制点的数作为精确的整数返回。
• 在 "Table" 格式, 列由空格或tabs分开。
• 在 "Words" 格式, 字由任何形式的空格分开。
Import["name.txt"] 使用 "Text" 格式.
Import["name.dat"] 使用 "Table" 格式.
• 对数值数据也支持下面的附加格式:
"\!\(\*StyleBox[\"\\\"\\\\\\\"HDF\\\\\\\"\\\"\", \"MR\"]\) "层级数据格式
"\!\(\*StyleBox[\"\\\"\\\\\\\"MAT\\\\\\\"\\\"\", \"MR\"]\) "MAT矩阵格式
• 二维图形格式作为Graphics对象引入;声音格式作为Sound对象引入。 • 动画图形作为Graphics对象列表引入。 • 下面的格式产生形式Graphics[data, opts]的表达式:
"\!\(\*StyleBox[\"\\\"\\\\\\\"EPS\\\\\\\"\\\"\", \"MR\"]\) "压缩PostScript
"\!\(\*StyleBox[\"\\\"\\\\\\\"EPSI\\\\\\\"\\\"\", \"MR\"]\) "有图象预览的压缩PostScript
"\!\(\*StyleBox[\"\\\"\\\\\\\"EPSTIFF\\\\\\\"\\\"\", \"MR\"]\) "有TIFF预览的压缩PostScript
"\!\(\*StyleBox[\"\\\"\\\\\\\"MPS\\\\\\\"\\\"\", \"MR\"]\) "Mathematica简化PostScript
• 下面的格式产生形式Graphics[Raster[data], opts]的表达式:
"\!\(\*StyleBox[\"\\\"\\\\\\\"BMP\\\\\\\"\\\"\", \"MR\"]\) "Microsoft 位图格式
"\!\(\*StyleBox[\"\\\"\\\\\\\"GIF\\\\\\\"\\\"\", \"MR\"]\) ""GIF 和动画 "
"\!\(\*StyleBox[\"\\\"\\\\\\\"JPEG\\\\\\\"\\\"\", \"MR\"]\) ""JPEG (\!\(\*StyleBox[\"\\\".jpg\\\"\", \"MR\"]\), \!\(\*StyleBox[\"\\\".jpeg\\\"\", \"MR\"]\)) "
"\!\(\*StyleBox[\"\\\"\\\\\\\"MGF\\\\\\\"\\\"\", \"MR\"]\) "Mathematica系统独立光栅图形格式
"\!\(\*StyleBox[\"\\\"\\\\\\\"PBM\\\\\\\"\\\"\", \"MR\"]\) "可移动位图格式
"\!\(\*StyleBox[\"\\\"\\\\\\\"PGM\\\\\\\"\\\"\", \"MR\"]\) "可移动灰度格式
"\!\(\*StyleBox[\"\\\"\\\\\\\"PNG\\\\\\\"\\\"\", \"MR\"]\) ""PNG format (\!\(\*StyleBox[\"\\\".png\\\"\", \"MR\"]\)) "
PNG格式
"\!\(\*StyleBox[\"\\\"\\\\\\\"PNM\\\\\\\"\\\"\", \"MR\"]\) "可移动anymap格式
"\!\(\*StyleBox[\"\\\"\\\\\\\"PPM\\\\\\\"\\\"\", \"MR\"]\) "可移动pixmap格式
"\!\(\*StyleBox[\"\\\"\\\\\\\"PSImage\\\\\\\"\\\"\", \"MR\"]\) "PostScript图形格式
"\!\(\*StyleBox[\"\\\"\\\\\\\"TIFF\\\\\\\"\\\"\", \"MR\"]\) ""TIFF (\!\(\*StyleBox[\"\\\".tif\\\"\", \"MR\"]\), \!\(\*StyleBox[\"\\\".tiff\\\"\", \"MR\"]\)) "
"\!\(\*StyleBox[\"\\\"\\\\\\\"XBitmap\\\\\\\"\\\"\", \"MR\"]\) """X窗口系统位图
• 引入的raster格式通常包含整数;ColorFunction经常用于指定一个颜色映射。
• 下面的格式产生形式 Sound[SampledSoundList[data, r]]的表达式:
"\!\(\*StyleBox[\"\\\"\\\\\\\"AIFF\\\\\\\"\\\"\", \"MR\"]\) "AIFF格式
"\!\(\*StyleBox[\"\\\"\\\\\\\"AU\\\\\\\"\\\"\", \"MR\"]\) "Mu""law编码
"\!\(\*StyleBox[\"\\\"\\\\\\\"SND\\\\\\\"\\\"\", \"MR\"]\) "声音文件格式
"\!\(\*StyleBox[\"\\\"\\\\\\\"WAV\\\\\\\"\\\"\", \"MR\"]\) "微软波""格式
• 可以给定下列一般选项:
"\!\(\*StyleBox[\"\\\"ByteOrdering\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"$ByteOrdering\\\"\", \"MR\"]\) "用于排序的二进制数据的字节数
"\!\(\*StyleBox[\"\\\"CharacterEncoding\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"Automatic\\\"\", \"MR\"]\) "用于文本中字符编码
"\!\(\*StyleBox[\"\\\"ConversionOptions\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"{}\\\"\", \"MR\"]\) "指定格式的私有选项
"\!\(\*StyleBox[\"\\\"Path\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"$Path\\\"\", \"MR\"]\) "搜索文件路径
• 由Import接受的可能的格式在列表$ImportFormats中给出。
Import["!prog", "format"] 从一个管道引入数据。
Additional Information

Many Import formats have private options which are specified using the option ConversionOptions: Import["file", "format", ConversionOptions ->  opts ].

GIF

• The following options can be given:
"\!\(\*StyleBox[\"\\\"\\\\\\\"ColormapOutput\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"True\\\"\", \"MR\"]\) ""determines how the \!\(\*StyleBox[\"\\\"Raster\\\"\", \"MR\"]\) data is imported "
确定raster数据如何引入
"\!\(\*StyleBox[\"\\\"\\\\\\\"ImageIndex\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"Automatic\\\"\", \"MR\"]\) ""selects certain frames of an animated GIF "
选择一个动画GIF的一定框架
• The option "ColormapOutput" determines whether a GIF is imported as a Raster graphics primitive with a color map ColorFunction and a matrix of indices into the color map, or whether the imported Raster graphics primitive contains a normal matrix of sample values. A grayscale image will import without the use of a color map ColorFunction.
• If the GIF is an animated GIF with multiple frames, "ImageIndex" -> i will select a particular frame to import. "ImageIndex" ->   ,  , ...  will select a list of frames from the animated GIF. By default all frames of an animated GIF are imported.

HDF

• The following option can be given:
"\!\(\*StyleBox[\"\\\"\\\\\\\"DatasetSelection\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"1\\\"\", \"MR\"]\) ""determines which dataset is imported "
确定引入那个数据集
• HDF files can contain multiple datasets in a single file. By default, the first dataset in an HDF file is imported. "DatasetSelection" -> i will attempt to import the dataset at index i. "DatasetSelection" -> string will attempt to find a dataset named with label string. "DatasetSelection" -> Information will not import the datasets in the file but instead return a list of length equal to the number of datasets in the file and contain information about the format types, names, and dimensions of the datasets.
• Import of HDF currently only supports the "scientific datasets" SDS interface.

JPEG

• The following options can be given:
"\!\(\*StyleBox[\"\\\"\\\\\\\"ColorReduction\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"None\\\"\", \"MR\"]\) ""determines whether color reduction occurs when importing "
"\!\(\*StyleBox[\"\\\"\\\\\\\"ColorReductionDither\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"\\\\\\\"Floyd-Steinberg\\\\\\\"\\\"\", \"MR\"]\) ""specifies dithering method used with color reduction "
"\!\(\*StyleBox[\"\\\"\\\\\\\"ColorReductionPalette\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"Automatic\\\"\", \"MR\"]\) ""determines colors in color reduced image "
"\!\(\*StyleBox[\"\\\"\\\\\\\"Colorspace\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"Automatic\\\"\", \"MR\"]\) ""specifies what color space is used for the imported image "
• Although the JPEG format is not stored as a color-mapped image, Import can color reduce or quantize the JPEG as it is being imported. By default no color reduction is done.
• Using a "ColorReductionDither" method can provide better resulting images once a color map is chosen by dithering the resulting image. "ColorReductionDither" -> "Ordered" provides relatively fast and medium quality dithering, while "ColorReductionDither" -> "Floyd-Steinberg" provides the best quality error-diffusion dithering, requiring more time and memory. "ColorReductionDither" -> None will perform no dithering on the imported image.
"ColorReduction" -> "Fixed" chooses evenly spaced colors from a color space, which may not pick the most optimal colors for the image. "ColorReduction" -> "Adaptive" uses a median-cut algorithm to choose the optimal set of colors for an image.
"ColorReductionPalette" -> Automatic will choose an adaptive set of at most 256 colors. "ColorReductionPalette" -> n will choose at most n colors for the color palette. "ColorReductionPalette" ->   ,  , ...  specifies a list of color directives to use to color reduce the image. The values for  must be CMYKColor, GrayLevel, Hue or RGBColor directives. The number of colors chosen for "ColorReductionPalette" must be between 2 and 256.
• The following example produces an image using a color map of "browser safe" 216 colors: Import["file.jpg", "JPEG", ConversionOptions -> {"ColorReduction" -> "Fixed", "ColorReductionPalette" -> 216}].
• Color reduction of JPEG images only applies to JPEG images with three color components. If you are producing these JPEG images yourself, you can force the use of three color components with Export and "Colorspace" -> "RGBColor": Export["file.jpg", gr, "JPEG", ConversionOptions->{"Colorspace" -> RGBColor}].
• The option "Colorspace" can be explicitly set to RGBColor or GrayLevel to force the resulting imported image to a specific color space.

List

• The following options can be given:
"\!\(\*StyleBox[\"\\\"\\\\\\\"CurrencyTokens\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"{{\\\\\\\"$\\\\\\\",\\\"\", \"MR\"]\) \!\(\*StyleBox[\"\\\"\\\\\\\"\[Sterling]\\\\\\\",\\\"\", \"MR\"]\) \!\(\*StyleBox[\"\\\"\\\\\\\"\[Yen]\\\\\\\"},\\\"\", \"MR\"]\) \!\(\*StyleBox[\"\\\"{\\\\\\\"c\\\\\\\",\\\"\", \"MR\"]\) \!\(\*StyleBox[\"\\\"\\\\\\\"p\\\\\\\", \\\\\\\"F\\\\\\\"}}\\\"\", \"MR\"]\) ""recognized currency characters "
"\!\(\*StyleBox[\"\\\"\\\\\\\"DateStyle\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"\\\\\\\"American\\\\\\\"\\\"\", \"MR\"]\) ""format of dates "
"\!\(\*StyleBox[\"\\\"\\\\\\\"DebitMark\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"\\\\\\\"-\\\\\\\"\\\"\", \"MR\"]\) ""debit character marker "
"\!\(\*StyleBox[\"\\\"\\\\\\\"ListSeparators\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"{\\\\\\\"\r\\\\\\\",\\\"\", \"MR\"]\) \!\(\*StyleBox[\"\\\"\\\\\\\"\n\\\\\\\",\\\"\", \"MR\"]\) \!\(\*StyleBox[\"\\\"\\\\\\\"\t\\\\\\\"}\\\"\", \"MR\"]\) ""list of characters that delimit lines "
"\!\(\*StyleBox[\"\\\"\\\\\\\"NumberPoint\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"\\\\\\\".\\\\\\\"\\\"\", \"MR\"]\) ""decimal point character "
• The option "DateStyle" can be set to the values "American", "European", or "Scientific".

PBM, PGM, PPM, PNM

• The following options can be given:
"\!\(\*StyleBox[\"\\\"\\\\\\\"CheckDepth\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"True\\\"\", \"MR\"]\) ""determines whether the image color depth is checked"
"\!\(\*StyleBox[\"\\\"\\\\\\\"ColorReduction\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"False\\\"\", \"MR\"]\) ""determines whether color reduction occurs when importing "
"\!\(\*StyleBox[\"\\\"\\\\\\\"ColorReductionDither\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"True\\\"\", \"MR\"]\) ""determines whether to use dithering "
"\!\(\*StyleBox[\"\\\"\\\\\\\"ColorReductionPalette\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"Automatic\\\"\", \"MR\"]\) ""determines colors in resulting image "
"CheckDepth" -> True will check the data on importing to see if the actual data stored in the image could be represented by a reduced color space. For example, if a PPM file contains identical red, green and blue components for all pixels, a Raster of graylevels is imported.
• Although the PBM formats are not stored as a color-mapped image, Import can color reduce or quantize the PBM as it is being imported. By default no color reduction is done. "ColorReduction" -> True will perform color quantization.
• By dithering the resulting image, "ColorReductionDither" -> True will provide better resulting images once a color map is chosen.
"ColorReductionPalette" -> Automatic will choose an adaptive set of at most 256 colors. "ColorReductionPalette" -> n will choose at most n colors for the color palette. "ColorReductionPalette" ->   ,  , ...  specifies a list of color directives to use to color reduce the image. The values for  must be CMYKColor, GrayLevel, Hue or RGBColor directives. The number of colors chosen for "ColorReductionPalette" must be between 2 and 256.

Table

• The following options can be given:
"\!\(\*StyleBox[\"\\\"\\\\\\\"CurrencyTokens\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"{{\\\\\\\"$\\\\\\\",\\\"\", \"MR\"]\) \!\(\*StyleBox[\"\\\"\\\\\\\"\[Sterling]\\\\\\\",\\\"\", \"MR\"]\) \!\(\*StyleBox[\"\\\"\\\\\\\"\[Yen]\\\\\\\",\\\"\", \"MR\"]\) \!\(\*StyleBox[\"\\\"\\\\\\\"\[Euro]\\\\\\\"},\\\"\", \"MR\"]\) \!\(\*StyleBox[\"\\\"{\\\\\\\"c\\\\\\\",\\\"\", \"MR\"]\) \!\(\*StyleBox[\"\\\"\\\\\\\"p\\\\\\\", \\\\\\\"F\\\\\\\"}}\\\"\", \"MR\"]\) ""recognized currency characters "
"\!\(\*StyleBox[\"\\\"\\\\\\\"DateStyle\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"\\\\\\\"American\\\\\\\"\\\"\", \"MR\"]\) ""format of dates "
"\!\(\*StyleBox[\"\\\"\\\\\\\"DebitMark\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"\\\\\\\"-\\\\\\\"\\\"\", \"MR\"]\) ""debit character marker "
"\!\(\*StyleBox[\"\\\"\\\\\\\"NumberPoint\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"\\\\\\\".\\\\\\\"\\\"\", \"MR\"]\) ""decimal point character "
"\!\(\*StyleBox[\"\\\"\\\\\\\"TableSeparators\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"{{\\\\\\\"\r\\\\\\\",\\\\\\\"\n\\\\\\\"},\\\"\", \"MR\"]\) \!\(\*StyleBox[\"\\\"{Automatic,\\\"\", \"MR\"]\) \!\(\*StyleBox[\"\\\"\\\\\\\"\t\\\\\\\"}}\\\"\", \"MR\"]\) ""list of characters that delimit records and fields "
"\!\(\*StyleBox[\"\\\"\\\\\\\"TwoDigitYearFunction\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"None\\\"\", \"MR\"]\) ""how two-digit years in date fields should be interpreted "
• The option "DateStyle" can be set to the values "American", "European", or "Scientific".
• A field is considered to be a date if it consists of three adjacent, forward slash-delimited integers of which the month, day, and year values appear to be reasonable. Months must be between 1 and 12 inclusive. The day must not lie outside the valid range of days for the given month. A year's validity can be determined indirectly by "TwoDigitYearFunction".
• A date field will be broken into a list of the form year, month, day.
• The option "TwoDigitYearFunction" can be set to None, Automatic, or an arbitrary function.
• Setting the "TwoDigitYearFunction" to its default value of None keeps the date field from being interpreted as a date if the year has only two-digits.
• When "TwoDigitYearFunction" is set to Automatic, 1900 is added to the year and the field is interpreted as a date.
• User-defined functions for "TwoDigitYearFunction" must return Integer values.

TIFF

• The following options can be given:
"\!\(\*StyleBox[\"\\\"\\\\\\\"CheckDepth\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"True\\\"\", \"MR\"]\) ""checks image color depth "
"\!\(\*StyleBox[\"\\\"\\\\\\\"ImageIndex\\\\\\\"\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"Automatic\\\"\", \"MR\"]\) ""selects certain images from a TIFF "
"CheckDepth" -> True will check the data on importing to see if the actual bit depth of the TIFF is smaller than one byte per image sample, and this can result in a Raster with integer values scaled to a more exact representation of what was stored in the TIFF. For example, a 4-bit TIFF image would be imported as a Raster with a matrix of integer values between 0 and 15 and not the normal 0 to 255 associated with a byte of image sample information.
• If the TIFF contains multiple images, "ImageIndex" -> i will select a particular image to import. "ImageIndex" ->   ,  , ...  will select a list of images from the TIFF. By default all images of a TIFF are imported.