Python¶
Doc: https://docs.python.org/dev/tutorial/index.html
datetime¶
datetime¶
A datetime object is a single object containing all the information from a date object and a time object.
[ins] In [27]: from datetime import datetime, timedelta
[ins] In [31]: dt = datetime.now()
[ins] In [32]: dt
Out[32]: datetime.datetime(2024, 8, 20, 14, 10, 8, 566237)
[ins] In [33]: dt.strftime("%Y-%m-%d %H:%M:%S")
Out[33]: '2024-08-20 14:10:08'
[ins] In [34]: datetime.strptime("20240820", "%Y%m%d")
Out[34]: datetime.datetime(2024, 8, 20, 0, 0)
| Method | strftime |
strptime |
|---|---|---|
| Usage | Convert object to a string according to a given format | Parse a string into a datetime object given a corresponding format |
| Type of method | Instance method | Class method |
| Method of | date; datetime; time |
datetime |
| Signature | strftime(format) |
strptime(date_string, format) |
timedelta¶
A timedelta object represents a duration, the difference between two datetime or date instances.
[ins] In [27]: from datetime import datetime, timedelta
[ins] In [28]: now = datetime.now()
[ins] In [29]: timedelta(days=365)
Out[29]: datetime.timedelta(days=365)
[ins] In [30]: now - timedelta(days=365)
Out[30]: datetime.datetime(2023, 8, 21, 14, 8, 22, 953271)
sys¶
sys.byteorder: An indicator of the native byte order. Edianness outputlittleorbig.
fileinput¶
-
Create an instance of the
FileInputclass.使用
readline()方法读取一行输入:
Built-in Functions¶
-
exec(): This function supports dynamic execution of Python code. object must be either a string or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs).输入一行字符串并作为 Python 语句执行:
-
globals(): Return a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).全局符号表
-
ord(c): Given a string representing one Unicode character, return an integer representing the Unicode code point of that characterc.chr(i): Return the string representing a character whose Unicode code point is the integeri.Unicode 字符与编码相互转换
-
format(value[, format_spec]): Convert a value to a “formatted” representation, as controlled by format_spec.bin(x): Convert an integer number to a binary string prefixed with “0b”.oct(x): Convert an integer number to an octal string prefixed with “0o”.hex(x): Convert an integer number to a lowercase hexadecimal string prefixed with “0x”.
IPython Tips¶
-
如果你不确定 function, class, or method 的使用方法,打印
__doc__属性,查看文档,或者在 IPython 中语句末尾加上一个 question mark?,两个??查看更详细的内容In [17]: print(spark.read.__doc__) Interface used to load a :class:`DataFrame` from external storage systems (e.g. file systems, key-value stores, etc). Use :attr:`SparkSession.read` to access this. .. versionadded:: 1.4.0 .. versionchanged:: 3.4.0 Supports Spark Connect. In [18]: spark.read? Type: property String form: <property object at 0x104841b20> Docstring: Returns a :class:`DataFrameReader` that can be used to read data in as a :class:`DataFrame`. .. versionadded:: 2.0.0 .. versionchanged:: 3.4.0 Supports Spark Connect. Returns ------- :class:`DataFrameReader` Examples -------- >>> spark.read <...DataFrameReader object ...> Write a DataFrame into a JSON file and read it back. >>> import tempfile >>> with tempfile.TemporaryDirectory() as d: ... # Write a DataFrame into a JSON file ... spark.createDataFrame( ... [{"age": 100, "name": "Hyukjin Kwon"}] ... ).write.mode("overwrite").format("json").save(d) ... ... # Read the JSON file as a DataFrame. ... spark.read.format('json').load(d).show() +---+------------+ |age| name| +---+------------+ |100|Hyukjin Kwon| +---+------------+