Tag:array eterm ted float in addition to Default values functions prim use scene
# -*- coding: utf-8 -*-' About numpy.genfromtxt, means generate from txt filehttps://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.htmlnumpy.genfromtxt ( fname, dtype=<type ' float ' >, comments= ' # ', delimiter=none, skip_header=0, skip_footer=0, converters=none, missing_values=none, filling_values =none, usecols=none, names=none, excludelist=None, Deletechars=none, replace_space= ' _ ', autostrip=false, case_sensitive=true, defAultfmt= ' f%i ', unpack=none, usemask=false, loose=true, invalid_raise=true, max_rows= none, encoding= ' bytes ') see also Numpy.loadtxt equivalent function when no data is missing. " From cryptography.hazmat.primitives.serialization import encoding# ndarray_fromtxt_lt = numpy.loadtxt (' data.txt ', delimiter= ', ', dtype=numpy.str) # # ndarray_fromtxt_lt = numpy.loadtxt (Open (' Data.txt ', ' R ', encoding= ' utf-8 '), delimiter= ', ', dtype=numpy.str) # # ndarray_ Fromtxt_lt = numpy.loadtxt (fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin) # print (Type (NDARRAY_FROMTXT_LT)) # print (Ndarray_ FROMTXT_LT) # ndarray_data = numpy. Genfromtxt (fname= ' data.txt ', dtype=str,delimiter, skip_header, skip_footer, converters, missing_values, filling_values, usecols, names, excludelist, deletechars, Replace_space, autostrip, case_sensitive, defaultfmt, unpack, usemask, loose, invalid_raise, max_rows) # file = open (' Data.txt ', encoding= ' utf-8 ') ' -Usage Scenarios: Data conversion (encoding conversion, value conversion) -key Parameters:converters the set of functions that convert the data of a column to a value. The Converters can also be used to provide a default value for missing data: converters = {3: lambda s: float (s or 0)}. -It is a collection of functions, you can write a function (or use lambda) to convert the value of a column, commonly used scenarios are: encoding conversion, value conversion, etc. -Special attention!!! - 1, the input of the conversion function, the default is the bytes type, is related to the encoding parameter, regardless of the Dtype parameter. - Dtype Impact Data Final presentation form - encoding influence Data processing process - official description of the encoding parameters: - 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 ' . - 2, the return type of the conversion function, must be consistent with the set Dtype, or it will cause unpredictable data loss. - for example, Genfromtxt set Dtype=str, that is, all columns are of type STR, the return type of the conversion function must also be str - 3, if the data contains Chinese, may conflict with Windows system default ASCII character set, need to transcode to Utf-8 "' Import osos.remove (' data.txt ') fo = open (' data.txt ', ' a ', encoding= ' Utf-8 ') fo.write ("001, Zhang San, man,24\n") Fo.write ("002, John Doe, man,24\n") Fo.close () Import numpydef convutf8 (x): return x.decode (' Utf-8 ') def Convadd (x): return str (x,encoding= ' utf-8 ') + ' + ' "' Correct demonstration: Single row processing ' ' Ndarry_1 = numpy.genfromtxt (fname= ' data.txt ', deliMiter= ', ',dtype=str, converters={1: convutf8}) Print (ndarry_1) # [[' 001 ' ' Zhang San ' ' man ' ']# [' 002 ' ' John Doe ' ' man ' ' 24 ']] "' Correct demonstration: Multi-column processing ' ' Ndarry_1 = numpy.genfromtxt ' (fname= ' data.txt ', delimiter= ', ',dtype=str, converters={1: convUTF8, 2: lambda x: x.decode (' utf-8 ') }) print (ndarry_1) # [[' 001 ' ' Zhang San ' ' man ' ']# [' 002' ' John Doe ' ' man ' ' 24 ']] "' ERROR demonstration: Because the return type of the conversion function is not consistent with the input type, it can result in data loss ' ndarry_1 = Numpy.genfromtxt (fname= ' data.txt ', delimiter= ', ',dtype=str, &NBSP;&NBSP;CONVERTERS={1:&NBSP;LAMBDA&NBSP;X:&NBSP;1}) print (ndarry_1) # [(', 1, ', ') (', 1, ', ') "Error demonstration: For a column, only one conversion function, and can only be processed once, set multiple times, is invalid" "ndarry_1 = Numpy.genfromtxt (fname= ' data.txt ', delimiter= ', ',dtype=str, converters={1: convUTF8, 0: convadd, 0: convadd}) print (ndarry_1) # [[' 001+ ' ' Zhang San ' ' man ' ']# [' 002+ ' ' John Doe ' ' man ' ' []] ' ' -Usage Scenarios: Formatting columns -key parameters:dtype - data type of the resulting array. - if none, the dtypes will be determined by the contents of each column, individually. - As an element in Ndarray, Dtype can set the data type ' os.remove (' Data.txt ') fo = open (' Data.txt ', ' a ', encoding= ' utf-8 ') Fo.write ("001,zhangsan,man,24\n") fo.write ("002,liSi,man,24\n ") fo.close ()" Error demonstration: Dtype=none, automatic format, but passable. For non-numeric columns, the default is bytes ' Ndarry_1 = numpy.genfromtxt (fname= ' data.txt ', delimiter= ', ', Dtype=none) print ( ndarry_1) # [(1, b ' Zhangsan ', b ' man ', 24) (2, b ' Lisi ', b ' man ', 24)] " Correct demonstration: All columns are set to a uniform data type. For the data that does not contain Chinese, dtype=str is possible, if it contains Chinese, in addition to set DTYPE=STR, but also with converters to do transcoding "" Ndarry_1 = numpy.genfromtxt (fname = ' Data.txt ', delimiter= ', ', dtype=str) print (ndarry_1) # [[' 001 ' ' Zhangsan ' ' man ' ']# ' [' 002 ' ' Lisi ' ' man ' ' 24 '] ' "' Correct demonstration: Set the data type on a per-column basis. Need to know more about the type of dtype. "' Ndarry_1 = numpy.genfromtxt (fname= ' data.txt ', delimiter= ', ', dtype=[(' C0 ', ' <i8 '), (' C1 ', ' <u32 ') ), (' C2 ', ' | S3 '), (' C3 ', ' F4 ')]) print (ndarry_1) # [(1, ' Zhangsan ', b ' man ', 24.0) (2, ' Lisi '), b ' man ', 24.0) ' -usage scenario: Data slicing -key parameters:dtype - data type of the resulting array. - if none, the dtypes will be determined by the contents of each column, individually. - as an element in Ndarray, Dtype can set the data type ' ' ' -usage Scenarios: Handling of default values -key parameters:dtype - data type of the resulting array. - if none, the dtypes will be determined by the contents of each column, individually. - As an element in Ndarray, Dtype can set the data type "" " -Use scene: Data slice -key parameter: Skip_header - start line -key parameters:max_rows - Maximum number of rows -key parameter:usecols - reserved column - Key parameter:comments (the execution order is final, first do row and column slices, then delete comment lines) - comment symbol. - If it is a comment at the beginning, the line will be discarded; - If it is a comment from another location in the line, it will be an error, but the field behind the comment symbol is missing. "' Os.remove (' Data.txt ') fo = open (' Data.txt ', ' a ', encoding= ' Utf-8 ') fo.write (" 001,zhangsan,man,24\n ") Fo.write ("002,lisi,man,24\n") fo.write ("#003, wangwu,man,24\n") fo.write ("004,chenhua,wom,18\n") Fo.close () " Correct demonstration: Pay attention to perform shun, first do row and column slices, then do delete Comment line ' ' ' Ndarry_1 = numpy.genfromtxt (fname= ' data.txt ', delimiter= ', ', DTYPE=STR, skip_header=1,max_rows=3, usecols=[0,2], comments= ' # ', ) print (ndarry_1) # [[' 002 ' ' man ']# [' 004 ' ' WOM '] [] ' -usage Scenario: Fill the missing value (when Dtype=none, fill the missing value This function better use, when dtype=str, this function does not take effect, want to grope again) - Key parameter:missing_values - marked as missing -key parameter: Filling _values - "Os.remove (' data.txt ') to fill the missing position fo = open ( ' Data.txt ', ' a ', encoding= ' Utf-8 ') fo.write ("10,11,,13\n") fo.write ("10,21,22,23\n") fo.write ("10,31,32,33\n") Fo.close () "Correct demonstration: A column is missing for specific settings (other columnsis the default missing), all columns are unified by default to fill ' ndarry_1 = numpy.genfromtxt ' (fname= ' data.txt ', delimiter= ', ',dtype=none, missing_values={0:10}, filling_values=999 ) Print (ndarry_1) # [[999 11 999 13]# [999 21 22 &NBSP;&NBSP;23]#&NBSP;&NBSP;[999&NBSP;&NBSP;31&NBSP;&NBSP;32&NBSP;&NBSP;33]] "' Correct demonstration: The absence of a column, the filling of a column ' ' Ndarry_1 = numpy.genfromtxt (fname= ' data.txt ', delimiter= ', ',dtype=none, &nBsp; missing_ values={0:10}, filling_values={0:777,2:999} ) print (ndarry_1) # [[777 11 999 13]# [777 21 22 23]# [777 31 &NBSP;&NBSP;32&NBSP;&NBSP;33]] "' Correct demonstration: All columns are missing for uniform setting (but default is missing or in effect) ' Ndarry_1 = numpy.genfromtxt (fname= ' Data.txt ', delimiter= ', ',dtype=none, missing_values =10, filling_values={0:777,2:999} ) print (ndarry_1) # [[777 11 999 13]# [777 &NBSP;21&NBSP;&NBSP;22&NBSP;&NBSP;23]#&NBSP;&NBSP;[777&NBSP;&NBSP;31&NBSP;&NBSP;32&NBSP;&NBSP;33]]
Python practiced hand, numpy.genfromtxt