ToString
![]() |
Transforms every element in an input collection to a string.
Examples
Any non-string element can be converted to a string using standard Python functions and methods.
Many I/O transforms, such as
textio.WriteToText
,
expect their input elements to be strings.
Example 1: Key-value pairs to string
The following example converts a (key, value)
pair into a string delimited by ','
.
You can specify a different delimiter using the delimiter
argument.
Output:
Example 2: Elements to string
The following example converts a dictionary into a string.
The string output will be equivalent to str(element)
.
import apache_beam as beam
with beam.Pipeline() as pipeline:
plant_lists = (
pipeline
| 'Garden plants' >> beam.Create([
['π', 'Strawberry', 'perennial'],
['π₯', 'Carrot', 'biennial'],
['π', 'Eggplant', 'perennial'],
['π
', 'Tomato', 'annual'],
['π₯', 'Potato', 'perennial'],
])
| 'To string' >> beam.ToString.Element()
| beam.Map(print))
Output:
Example 3: Iterables to string
The following example converts an iterable, in this case a list of strings,
into a string delimited by ','
.
You can specify a different delimiter using the delimiter
argument.
The string output will be equivalent to iterable.join(delimiter)
.
import apache_beam as beam
with beam.Pipeline() as pipeline:
plants_csv = (
pipeline
| 'Garden plants' >> beam.Create([
['π', 'Strawberry', 'perennial'],
['π₯', 'Carrot', 'biennial'],
['π', 'Eggplant', 'perennial'],
['π
', 'Tomato', 'annual'],
['π₯', 'Potato', 'perennial'],
])
| 'To string' >> beam.ToString.Iterables()
| beam.Map(print))
Output:
Related transforms
- Map applies a simple 1-to-1 mapping function over each element in the collection
![]() |
Last updated on 2023/05/31
Have you found everything you were looking for?
Was it all useful and clear? Is there anything that you would like to change? Let us know!