Calls :meth:`str.encode` element-wise. The set of available codecs comes from the Python standard library, and may be extended at runtime. For more information, see the :mod:`codecs` module. Parameters ---------- a : array_like, with ``StringDType`` or ``str_`` dtype
(a, encoding=None, errors=None)
| 584 | @set_module("numpy.strings") |
| 585 | @array_function_dispatch(_code_dispatcher) |
| 586 | def encode(a, encoding=None, errors=None): |
| 587 | """ |
| 588 | Calls :meth:`str.encode` element-wise. |
| 589 | |
| 590 | The set of available codecs comes from the Python standard library, |
| 591 | and may be extended at runtime. For more information, see the |
| 592 | :mod:`codecs` module. |
| 593 | |
| 594 | Parameters |
| 595 | ---------- |
| 596 | a : array_like, with ``StringDType`` or ``str_`` dtype |
| 597 | |
| 598 | encoding : str, optional |
| 599 | The name of an encoding |
| 600 | |
| 601 | errors : str, optional |
| 602 | Specifies how to handle encoding errors |
| 603 | |
| 604 | Returns |
| 605 | ------- |
| 606 | out : ndarray |
| 607 | |
| 608 | See Also |
| 609 | -------- |
| 610 | str.encode |
| 611 | |
| 612 | Notes |
| 613 | ----- |
| 614 | The type of the result will depend on the encoding specified. |
| 615 | |
| 616 | Examples |
| 617 | -------- |
| 618 | >>> import numpy as np |
| 619 | >>> a = np.array(['aAaAaA', ' aA ', 'abBABba']) |
| 620 | >>> np.strings.encode(a, encoding='cp037') |
| 621 | array([b'\x81\xc1\x81\xc1\x81\xc1', b'@@\x81\xc1@@', |
| 622 | b'\x81\x82\xc2\xc1\xc2\x82\x81'], dtype='|S7') |
| 623 | |
| 624 | """ |
| 625 | return _to_bytes_or_str_array( |
| 626 | _vec_string(a, np.object_, 'encode', _clean_args(encoding, errors)), |
| 627 | np.bytes_(b'')) |
| 628 | |
| 629 | |
| 630 | def _expandtabs_dispatcher(a, tabsize=None): |
searching dependent graphs…