python - Pyspark - Python3 使用 configparser 从文件中获取变量

标签 python python-3.x apache-spark pyspark configparser

我试图使用 Configparser 从文件中获取变量,但它总是返回字符串而不是变量。请帮忙

config.ini

[db]
connection_sting =sqlContext.read.format(driver).load(table_nm)

config_conn = ConfigParser() 
conn_string = config_conn.get('db', 'connection_sting')

当前结果:

conn_string = 'sqlContext.read.format(driver).load(table_nm)'

预期:

conn_string = sqlContext.read.format(driver).load(table_nm)

最佳答案

只是不要尝试。配置文件用于提供配置选项,而不是可执行代码。

相反

config.ini

[db]
driver = some_format
table_nm = some_table

config = configparser.ConfigParser()
config.read("config.ini")

connection_sting = (sqlContext.read
    .format(config.get("db", "driver")
    .load(config.get("db", "table_nm")))

如果您需要可执行代码,请使用正确的模块,而不是配置。

关于python - Pyspark - Python3 使用 configparser 从文件中获取变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48645148/

相关文章:

garbage-collection - 您如何管理临时目录以确保在程序关闭时将其删除?

python-3.x - 如何在python3中创建asyncio对象?

python - 在随机网络x图中设置不同的节点颜色

scala - InvalidRequestException(为什么 :empid cannot be restricted by more than one relation if it includes an Equal)

python - 有没有一个Python函数可以获取真实社区?

python - 如何从 (ba)sh 脚本导入变量?

python - Scrapy:下载器/response_count 与 response_received_count

python - 在计算 networkx 中的传出和传入边缘时,len 抛出 'dict_keyiterator' 没有 len()

apache-spark - 在 Spark 中对多列求和

scala - Spark DataFrame 中的条件连接