首页 > 解决方案 > 从变量传递模式名称以在 Pyspark 中创建数据框

问题描述

尝试编写一个函数来创建数据框,其中架构名称作为参数传递给函数。

def extract_file(self, file_location={}):
    for (key, value) in file_location.items():
        print(key, " :: ", value)
        schema_name = 'schema_' + key
        # read the file and create a data frame
        df = self.spark.read \
            .format('csv') \
            .options(header='true', delimiter='|') \
            .schema(schema_name) \
            .load(exec('"{}"'.format(value)))

上面的代码不起作用,因为它需要不带引号的 schema 参数。如何从不带引号的存储字符串的变量中传递参数?

标签: pythonapache-sparkpyspark

解决方案


推荐阅读