我有一個存儲在變量List_dicts中的字典項目列表,我必須從上面的列表中選擇一個項目列表寫入一個文件。下面給出我的python函數: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動態執行quires中受到啓發。
使用CSV與dictwriter – thkang 2013-04-23 16:46:09