This topic describes how to use Chinese characters and time in OSS SDK for Python.

Chinese characters

To use Chinese characters in Python code, you must include a character encoding declaration in the beginning of the code. Otherwise, errors occur when you run the code. Example of a character encoding statement:

# -*- coding: utf-8 -*-
            
  • Data types

    The following table describes the data types supported by Python 2.x.

    Data typeDescription
    strString. It corresponds to the bytes type in Python 3.x.
    unicodeUnicode streams. The length is calculated based on the number of Chinese characters. For example, the length of u'中文' is 2.

    The following table describes the data types supported by Python 3.x.

    Data typeDescription
    strString. It corresponds to the unicode type in Python 2.x.
    bytesByte streams. The length is calculated based on the number of bytes. For example, the length of b'中文' is calculated based on the encoding mode. If UTF-8 is used, the length is 6 bytes.
  • Input and output limits

    The following table describes the limits on input data.

    InputTypeRemarks
    OSS object namestrIf the input data type is bytes, it must be encoded in UTF-8.
    Local file namestr, unicodeIf the input data type is bytes, it must be encoded in UTF-8. Example: the yourLocalFile parameter value used in bucket.get_object_to_file.
    Input data streambytesExample: data in bucket.put_object.

    The following table describes limits on output data.

    OutputTypeRemarks
    The result obtained by parsing the XML documentstrExample: a string in the result obtained by using bucket.list_object.
    Downloaded contentbytesBy default, data of the bytes type used by OSS SDK for Python must be encoded in UTF-8. Therefore, ensure that the source Python file is encoded in UTF-8.
  • Data type conversion functions

    OSS SDK for Python provides three functions for type conversion:

    FunctionDescription
    to_bytes

    - Converts the unicode type to the str type in Python 2.x. Data of other types is returned without changes.

    - Converts the str type to the bytes type in Python 3.x. Data of other types is returned without changes.

    to_unicode

    - Converts the str type to the unicode type in Python 2.x. Data of other types is returned without changes.

    - Converts the bytes type to the str type in Python 3.x. Data of other types is returned without changes.

    to_stringThis function is equivalent to to_bytes in Python 2.x. This function is equivalent to to_unicode in Python 3.x.

Time

OSS SDK for Python converts timestamp strings of the datetime.datetime type that are obtained from the server to a Unix timestamp corresponding to the amount of time in seconds elapsed since UTC 00:00 on January 1, 1970. For example, the last_modified parameter returned for bucket.get_object is an int Unix timestamp.

You can use the datetime.datetime.fromtimestamp() method to convert time to obtain timestamp strings.