edit-icon download-icon

Chinese character and time

Last Updated: Nov 07, 2017

Chinese character

The following table describes the list of terms that will be used soon:

Term Description
str Specifies the default string type in Python. It is the bytes type in Python 2.x, and the unicode type in Python 3.x.
bytes Specifies a byte stream. Its length is the number of bytes. For example, the length of chinese is determined by the encoding mode. If it is encoded with UTF-8, its length is 6.
unicode Specifies a unicode stream. Its length is the number of characters. For example, the length of chinese2 is 2.

Input and output conventions

The Python SDK has three types of input parameters:

Input parameter Type recommended Note
OSS object name str If it is the bytes type, it needs to be encoded with UTF-8.
Local file name str, unicode If the parameter is set to bytes, it needs to be encoded with UTF-8.
Input data stream bytes For example, the data parameter in Bucket.put_object.

The Local file name indicates the local file name parameter in, for example, Bucket.get_object_to_file.

The Python SDK has two types of outputs:

Output Type
Results of XML parsing str
Downloaded content bytes

The Results of XML parsing indicates the strings in the results obtained from the interfaces such as Bucket.list_objects and Bucket.get_bucket_lifecycle.

Python SDK regards the bytes type is encoded with UTF-8 by default, make sure that the Python source file is also encoded with UTF-8.

Helper functions

The Python SDK provides three functions to help you with type conversion:

Function Description
to_bytes Convert the unicode type to the bytes type encoded with UTF-8. For the other types, the original values are returned.
to_unicode Convert the bytes type encoded with UTF-8 to the unicode type. For the other types, the original values are returned.
to_string It equals to to_bytes in Python 2.x, and equals to to_unicode in Python 3.x.


The Python SDK converts all timestamp strings retrieved from the server to Unix Time, that is, the number of seconds since UTC 00:00 on January 1, 1970. For example, the last_modified time in the returned result for Bucket.get_object is the Unix Time of the int type.

To obtain a type like datetime.datetime, you can use methods such as datetime.datetime.fromtimestamp() to convert the value.

Thank you! We've received your feedback.