Struct Schema Creator for PySpark
[<Column Name>, <Column Type>, <Column Nullable>]
Types: binary, boolean, byte, date,
double, integer, long, null,
short, string, timestamp, unknown
from pyspark.sql.types import *
## Fill in with your desired column names, types, and nullability
cols = [["col1", "string", False],
["col3", "integer", True]]
## Loop to build list of StructFields
schema_set = ["schema = StructType(["]
for i, col in enumerate(cols):
coltype = col[1].title() + "Type()"
iter_structfield = "StructField('" + colname + "', " + coltype + ", " + str(colnull) + ")])"
iter_structfield = "StructField('" + colname + "', " + coltype + ", " + str(colnull) + "),"
schema_set.append(iter_structfield)
## Convert list to single string
schema_string = ''.join(map(str, schema_set))
## This will execute the generated command string