変数 List_dicts に保存されている辞書項目のリストがあり、上記のリストから選択した項目のリストをファイルに書き込む必要があります。私のpython関数の下に与えられた:
def write_items(List_dicts,key_list):
#List_dicts contains the list if dicts
#key_list contains the key separated by comma
k_list=key_list.split(',')
write_string=''
for i in k_list:
write_string=write_string+"item['"+i+"'],"
f=open('log.txt','w')
for item in List_dicts:
f.write(write_string[0:len(write_string)-1]) #this should write item['key1'],item['key2'],item['key3']..,item['keyn'] not the value of string 'write_string'
f.close()
とにかくこれは可能ですか?私は、SQL 動的実行クワイアから着想を得ました。