Skip to main content

text (DataFrameWriter)

Saves the content of the DataFrame in a text file at the specified path. Text files are encoded as UTF-8.

Syntax​

text(path, compression=None, lineSep=None)

Parameters​

Parameter

Type

Description

path

str

The path in any Hadoop-supported file system.

compression

str, optional

The compression codec to use.

lineSep

str, optional

The line separator to use.

Parameter

Type

Description

path

str

The path in any Hadoop-supported file system.

compression

str, optional

The compression codec to use.

lineSep

str, optional

The line separator to use.

Returns​

None

Notes​

The DataFrame must have only one column of string type. Each row becomes a new line in the output file.

Examples​

Write a DataFrame into a text file and read it back.

Python
import tempfile
with tempfile.TemporaryDirectory(prefix="text") as d:
df = spark.createDataFrame([("a",), ("b",), ("c",)], schema=["alphabets"])
df.write.mode("overwrite").text(d)

spark.read.schema(df.schema).format("text").load(d).sort("alphabets").show()
# +---------+
# |alphabets|
# +---------+
# | a|
# | b|
# | c|
# +---------+