PySpark Set 变量,即当前循环值的名称

  • 本文关键字:循环 Set 变量 PySpark pyspark
  • 更新时间 :
  • 英文 :


我想在循环中旋转配置文件,将我找到的任何选项分配给笔记本中同名的变量。所以代码更短,我没有太多的try和else步骤。我在开始时初始化默认选项,然后如果我找到一个同名的配置选项,它会更新它

配置文件

[file_options]
cfgfilename = 'newfilename.csv'

笔记本

import configparser
# default options
cfgfilename = 'dummy.csv'
otheroptions_etc = 'hi'
config = configparser.ConfigParser()
config.read({config file path here})
if config.has_section('file_options'): 
for option in config.options('file_options'):
{something here to set cfgfilename}= config.get('file_options', option )
print (cfgfilename)  # and so it comes out as newfilename.csv not dummy.csv

谢谢samKart这就是我使用字典解决问题的方法

# create dictionary with the dummy value pairs
dict_values = { 'filename' : 'dummy.csv',
'otheroptions_etc' : 'hi'}
# check it has a file options section
if config.has_section('file_options'): 
# loop through the options in the section
for option in config.options('file_options'):
# if that option is in the dictionary..
if option in dict_values.keys():
# ... up date it
dict_values[option] = config.get('file_options', option )
# finally set all the variables to the updated dictionary values
filename= dict_values['filename']
otheroptions_etc= dict_values['otheroptions_etc']

最新更新