Handle Chinese characters and time (Python SDK V1)

Updated at:
Copy as MD

Resolve Chinese character encoding errors and convert UNIX timestamps in OSS Python SDK V1.

Chinese characters

Python code with Chinese characters raises a runtime error unless you declare character encoding at the top of the file:

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

    Python 2 supports two data types:

    Data type

    Description

    str

    A string. Corresponds to the bytes type in Python 3.

    unicode

    A Unicode stream. Length equals the character count. For example, u'éà' has a length of 2.

    Python 3 supports two data types:

    Data type

    Description

    str

    A string. Corresponds to the unicode type in Python 2.

    bytes

    A byte stream. Length equals the byte count. For example, b'Chinese' has a length of 6 in UTF-8.

  • Input and output type conventions

    Input type conventions:

    Input

    Type

    Notes

    OSS file name

    str

    Must be UTF-8 encoded if bytes.

    Local file name

    str, unicode

    Must be UTF-8 encoded if bytes. Example: yourLocalFile in bucket.get_object_to_file.

    Input data stream

    bytes

    Example: data in bucket.put_object.

    Output type conventions:

    Output

    Type

    Notes

    Result of XML parsing

    str

    Example: strings in bucket.list_object results.

    Downloaded content

    bytes

    The SDK uses UTF-8 encoding by default. Ensure your source file is UTF-8 encoded.

  • Type conversion functions

    The SDK provides three type conversion functions:

    Function

    Description

    to_bytes

    - Python 2: converts unicode to str. Other types unchanged.

    - Python 3: converts str to bytes. Other types unchanged.

    to_unicode

    - Python 2: converts str to unicode. Other types unchanged.

    - Python 3: converts bytes to str. Other types unchanged.

    to_string

    Equivalent to to_bytes in Python 2 and to_unicode in Python 3.

Time

The SDK converts server-returned datetime.datetime values to UNIX timestamps (seconds since 00:00:00 UTC, January 1, 1970). For example, last_modified returned by bucket.get_object is an integer UNIX timestamp.

Use datetime.datetime.fromtimestamp() to convert a UNIX timestamp to a datetime object.