python - numpy.loadtxt文档翻译

it2026-06-16  4

才发现原来有中文网,而且刚才是搜dtype才去的那! https://www.numpy.org.cn/ 但可惜loadtxt还是没有中文文档

%%writefile test.txt 1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7

Help on function loadtxt in module numpy:

loadtxt(fname, dtype=<class ‘float’>, comments=’#’, delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding=‘bytes’, max_rows=None) Load data from a text file.

Each row in the text file must have the same number of values. 文本文件中的每一行必须具有相同数量的值。 Parameters ---------- fname : file, str, or pathlib.Path File, filename, or generator to read. If the filename extension is ``.gz`` or ``.bz2``, the file is first decompressed. Note that generators should return byte strings. 文件名(若没在同一目录,注意路径)、文件名生成器(pathlib.Path) 如果文件是打包文件,需要先解压。生成器应返回字节字符串 支持StringIO,见下例 dtype : data-type, optional Data-type of the resulting array; default: float. If this is a structured data-type, the resulting array will be 1-dimensional, and each row will be interpreted as an element of the array. In this case, the number of columns used must match the number of fields in the data-type. 数据类型,可选,默认浮点数,如果是结构类型,则生成一维数组,一行即一个元素,注意所使用的列数与字段数匹配 实际书写时,不需要<class 'float'>而是直接写为float、str、int、list等 见例 comments : str or sequence of str, optional The characters or list of characters used to indicate the start of a comment. None implies no comments. For backwards compatibility, byte strings will be decoded as 'latin1'. The default is '#'. 注释,字符或字符串("1 2"),可选,默认"#",单行首字符串。字符串当做'latin1'编码解码 Latin1是ISO-8859-1的别名 delimiter : str, optional The string used to separate values. For backwards compatibility, byte strings will be decoded as 'latin1'. The default is whitespace. 分隔符,字符串,可选,默认空格 converters : dict, optional A dictionary mapping column number to a function that will parse the column string into the desired value. E.g., if column 0 is a date string: ``converters = {0: datestr2num}``. Converters can also be used to provide a default value for missing data (but see also `genfromtxt`): ``converters = {3: lambda s: float(s.strip() or 0)}``. Default: None. 转换器,字典,可选,或者过滤器,或函数,默认无??? skiprows : int, optional Skip the first `skiprows` lines, including comments; default: 0. 跳过前面几句,可选,默认0 usecols : int or sequence, optional Which columns to read, with 0 being the first. For example, ``usecols = (1,4,5)`` will extract the 2nd, 5th and 6th columns. The default, None, results in all columns being read. 要读取的列,数字或序列,可选,默认None是读取所有列 .. versionchanged:: 1.11.0 When a single column has to be read it is possible to use an integer instead of a tuple. E.g ``usecols = 3`` reads the fourth column the same way as ``usecols = (3,)`` would. 以上两种方式效果一样 unpack : bool, optional If True, the returned array is transposed, so that arguments may be unpacked using ``x, y, z = loadtxt(...)``. When used with a structured data-type, arrays are returned for each field. Default is False. 解包,可选,返回值将分别赋值给x、y、z,默认False ndmin : int, optional The returned array will have at least `ndmin` dimensions. Otherwise mono-dimensional axes will be squeezed. Legal values: 0 (default), 1 or 2. ndmin维度,返回数组至少有ndmin维度,否则被压缩,默认0,可写1、2 .. versionadded:: 1.6.0 encoding : str, optional Encoding used to decode the inputfile. Does not apply to input streams. The special value 'bytes' enables backward compatibility workarounds that ensures you receive byte arrays as results if possible and passes 'latin1' encoded strings to converters. Override this value to receive unicode arrays and pass strings as input to converters. If set to None the system default is used. The default value is 'bytes'. 编码 .. versionadded:: 1.14.0 max_rows : int, optional Read `max_rows` lines of content after `skiprows` lines. The default is to read all the lines. 读取行数,可选,默认所有 .. versionadded:: 1.16.0 Returns ------- out : ndarray Data read from the text file. See Also -------- load, fromstring, fromregex genfromtxt : Load data with missing values handled as specified. scipy.io.loadmat : reads MATLAB data files Notes ----- This function aims to be a fast reader for simply formatted files. The `genfromtxt` function provides more sophisticated handling of, e.g., lines with missing values. .. versionadded:: 1.10.0 The strings produced by the Python float.hex method can be used as input for floats. Examples -------- >>> from io import StringIO # StringIO behaves like a file object >>> c = StringIO(u"0 1\n2 3") >>> np.loadtxt(c) array([[0., 1.], [2., 3.]]) >>> d = StringIO(u"M 21 72\nF 35 58") >>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'), ... 'formats': ('S1', 'i4', 'f4')}) array([(b'M', 21, 72.), (b'F', 35, 58.)], dtype=[('gender', 'S1'), ('age', '<i4'), ('weight', '<f4')]) >>> c = StringIO(u"1,0,2\n3,0,4") >>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True) >>> x array([1., 3.]) >>> y array([2., 4.])

结构化数组

numpy.loadtxt() 详细用法

关于python中numpy.loadtxt()的详细用法

numpy中loadtxt 的用法

最新回复(0)