@@ -815,7 +815,7 @@ def _preprocess_comments(iterable, comments, encoding):
815815def _read (fname , * , delimiter = ',' , comment = '#' , quote = '"' ,
816816 imaginary_unit = 'j' , usecols = None , skiplines = 0 ,
817817 max_rows = None , converters = None , ndmin = None , unpack = False ,
818- dtype = np .float64 , encoding = "bytes" ):
818+ dtype = np .float64 , encoding = None ):
819819 r"""
820820 Read a NumPy array from a text file.
821821 This is a helper function for loadtxt.
@@ -1073,7 +1073,7 @@ def _read(fname, *, delimiter=',', comment='#', quote='"',
10731073@set_module ('numpy' )
10741074def loadtxt (fname , dtype = float , comments = '#' , delimiter = None ,
10751075 converters = None , skiprows = 0 , usecols = None , unpack = False ,
1076- ndmin = 0 , encoding = 'bytes' , max_rows = None , * , quotechar = None ,
1076+ ndmin = 0 , encoding = None , max_rows = None , * , quotechar = None ,
10771077 like = None ):
10781078 r"""
10791079 Load data from a text file.
@@ -1145,6 +1145,10 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
11451145 the system default is used. The default value is 'bytes'.
11461146
11471147 .. versionadded:: 1.14.0
1148+ .. versionchanged:: 2.0
1149+ Before NumPy 2, the default was ``'bytes'`` for Python 2
1150+ compatibility. The default is now ``None``.
1151+
11481152 max_rows : int, optional
11491153 Read `max_rows` rows of content after `skiprows` lines. The default is
11501154 to read all the rows. Note that empty rows containing no data such as
@@ -1262,24 +1266,22 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
12621266 array([1.e+00, 2.7e+00, 1.e+05])
12631267
12641268 This idea can be extended to automatically handle values specified in
1265- many different formats:
1269+ many different formats, such as hex values :
12661270
12671271 >>> def conv(val):
12681272 ... try:
12691273 ... return float(val)
12701274 ... except ValueError:
12711275 ... return float.fromhex(val)
12721276 >>> s = StringIO("1, 2.5, 3_000, 0b4, 0x1.4000000000000p+2")
1273- >>> np.loadtxt(s, delimiter=",", converters=conv, encoding=None )
1277+ >>> np.loadtxt(s, delimiter=",", converters=conv)
12741278 array([1.0e+00, 2.5e+00, 3.0e+03, 1.8e+02, 5.0e+00])
12751279
1276- Note that with the default ``encoding="bytes"``, the inputs to the
1277- converter function are latin-1 encoded byte strings. To deactivate the
1278- implicit encoding prior to conversion, use ``encoding=None``
1280+ Or a format where the ``-`` sign comes after the number:
12791281
12801282 >>> s = StringIO('10.01 31.25-\n19.22 64.31\n17.57- 63.94')
12811283 >>> conv = lambda x: -float(x[:-1]) if x.endswith('-') else float(x)
1282- >>> np.loadtxt(s, converters=conv, encoding=None )
1284+ >>> np.loadtxt(s, converters=conv)
12831285 array([[ 10.01, -31.25],
12841286 [ 19.22, 64.31],
12851287 [-17.57, 63.94]])
@@ -1715,7 +1717,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
17151717 deletechars = '' .join (sorted (NameValidator .defaultdeletechars )),
17161718 replace_space = '_' , autostrip = False , case_sensitive = True ,
17171719 defaultfmt = "f%i" , unpack = None , usemask = False , loose = True ,
1718- invalid_raise = True , max_rows = None , encoding = 'bytes' ,
1720+ invalid_raise = True , max_rows = None , encoding = None ,
17191721 * , ndmin = 0 , like = None ):
17201722 """
17211723 Load data from a text file, with missing values handled as specified.
@@ -1816,6 +1818,10 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
18161818 The default value is 'bytes'.
18171819
18181820 .. versionadded:: 1.14.0
1821+ .. versionchanged:: 2.0
1822+ Before NumPy 2, the default was ``'bytes'`` for Python 2
1823+ compatibility. The default is now ``None``.
1824+
18191825 ndmin : int, optional
18201826 Same parameter as `loadtxt`
18211827
@@ -1858,7 +1864,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
18581864
18591865 Comma delimited file with mixed dtype
18601866
1861- >>> s = StringIO(u "1,1.3,abcde")
1867+ >>> s = StringIO("1,1.3,abcde")
18621868 >>> data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'),
18631869 ... ('mystring','S5')], delimiter=",")
18641870 >>> data
@@ -1885,7 +1891,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None,
18851891
18861892 An example with fixed-width columns
18871893
1888- >>> s = StringIO(u "11.3abcde")
1894+ >>> s = StringIO("11.3abcde")
18891895 >>> data = np.genfromtxt(s, dtype=None, names=['intvar','fltvar','strvar'],
18901896 ... delimiter=[1,3,5])
18911897 >>> data
0 commit comments