Chinese character and time

Last Updated: Oct 10, 2017

Chinese character

For convenience, the terms that will be used soon are defined and described first in the following table:

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

Input and output type 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.

Because the Python SDK regards that 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.

Time

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.