使詞典
列表
# Create an empty list at the beginning
list_of_dicts = []
# Then, for each dict you create...
d = { 'id' : counter,
'file': [ {'name': filename,
'uri' : 'http://localhost:8000/uploads/' + filename,
'path' : os.path.join(dirname, filename) } ] }
# ... add it to your list
list_of_dicts.append(d)
UPDATE:
我錯過了你有你的字典中的列表。如果要追加到列表,而不是,那麼就由'file'
鍵從字典獲取列表:
# The dict, with the first file
d = { 'id' : counter,
'file': [ {'name': filename,
'uri' : 'http://localhost:8000/uploads/' + filename,
'path' : os.path.join(dirname, filename) } ] }
# Then append all files you want to the list found in d['file']
d['file'].append({'name': some_other_filename,
'uri': some_other_uri,
'path': some_other_path})
你可以把一個文件的每'id'映射到這個'D'字典 – Jae