pyspark.sql.functions.url_encode#

pyspark.sql.functions.url_encode(str)[source]#

URL function: Encodes a string into a URL-encoded string in ‘application/x-www-form-urlencoded’ format.

New in version 3.5.0.

Parameters
strColumn or str

A column of strings, each representing a string to be URL-encoded.

Returns
Column

A new column of strings, each representing the URL-encoded string.

Examples

Example 1: Encoding a simple URL

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([("https://spark.apache.org",)], ["url"])
>>> df.select(sf.url_encode(df.url)).show(truncate=False)
+------------------------------+
|url_encode(url)               |
+------------------------------+
|https%3A%2F%2Fspark.apache.org|
+------------------------------+

Example 2: Encoding a URL with spaces

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([("Hello World!",)], ["url"])
>>> df.select(sf.url_encode(df.url)).show()
+---------------+
|url_encode(url)|
+---------------+
| Hello+World%21|
+---------------+

Example 3: Encoding a URL with special characters

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([("A+B==",)], ["url"])
>>> df.select(sf.url_encode(df.url)).show()
+---------------+
|url_encode(url)|
+---------------+
|    A%2BB%3D%3D|
+---------------+

Example 4: Encoding a URL with non-ASCII characters

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([("你好",)], ["url"])
>>> df.select(sf.url_encode(df.url)).show()
+------------------+
|   url_encode(url)|
+------------------+
|%E4%BD%A0%E5%A5%BD|
+------------------+

Example 5: Encoding a URL with hexadecimal values

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([("~!@#$%^&*()_+",)], ["url"])
>>> df.select(sf.url_encode(df.url)).show(truncate=False)
+-----------------------------------+
|url_encode(url)                    |
+-----------------------------------+
|%7E%21%40%23%24%25%5E%26*%28%29_%2B|
+-----------------------------------+