如何将词典列表添加到另一个词典列表?

时间:2011-10-24 11:40:24

标签: python list dictionary

我正在使用for循环将字典添加到字典列表(我正在调用data_file),使用data_file.append()

它工作正常:)但后来我尝试在另一个for循环中向data_file添加更多字典,我再次使用data_file.append()并且它不起作用。它不会将这些词典添加到data_file

有谁知道我做错了什么?

我没有收到错误。它只生成一个只包含massage_generators字典的文件。它不会从travel_generators中获取任何内容。

我甚至尝试过注释第一个for循环,一个用于massage_generators,并且在这种情况下它确实添加了travel_generators词典。这就像我不能两次使用.append()?

任何帮助都会非常感谢!

对不起它不是很优雅,我只是在学习这些编码的东西! :)

由于

import csv
import copy
import os
import sys


generators = list()

for filename in os.listdir(os.getcwd()):
    root, ext = os.path.splitext(filename)
    if root.startswith('Travel Allowance Auto Look up') and ext == '.csv':
      travel = filename

open_file = csv.DictReader(open(travel))
generators.append(open_file)    

travel_generators = generators[:]
massage_generators = generators[:]

data_file = []  # result will be stored here (List of dicts)


travel_remap = {'FINAL_TRAVEL_ALL':'AMOUNT'}
massage_remap = {'MASSAGE_BIK':'AMOUNT'}


for generator in massage_generators:
  for dictionary in generator:
    dictionary['PAYMENT_TYPE_CODE'] = 'MASSAGE_BIK'
    dictionary['COMMENT'] = 'Massage BIK'
    dictionary['IS_GROSS'] = 'FALSE'
    dictionary['PAYMENT_TO_DATE'] = '01/01/2099'
    dictionary['PAID MANUALLY'] = 'FALSE'
    for old_key, new_key in massage_remap.iteritems():
      if old_key not in dictionary:
        continue
      dictionary['AMOUNT'] = dictionary['MASSAGE_BIK']
      del dictionary['MASSAGE_BIK']
      if (dictionary['AMOUNT'] != '0' and dictionary['AMOUNT'] != ''):
          data_file.append(dictionary)

for generator in travel_generators:
  for dictionary in generator:
    dictionary['PAYMENT_TYPE_CODE'] = 'TRANSPORTATION_ALLOWANCE'
    dictionary['COMMENT'] = 'Annual travel allowance paid in monthly installments'
    dictionary['IS_GROSS'] = 'TRUE'
    dictionary['PAYMENT_TO_DATE'] = '01/01/2099'
    dictionary['PAID MANUALLY'] = 'FALSE'
    for old_key, new_key in travel_remap.iteritems():
      if old_key not in dictionary:
        continue
      dictionary['AMOUNT'] = dictionary['FINAL_TRAVEL_ALL']
      del dictionary['FINAL_TRAVEL_ALL']
      if (dictionary['AMOUNT'] != 'Not Applicable' and dictionary['AMOUNT'] != '0' and dictionary['AMOUNT'] != '' and dictionary['AMOUNT'] != '#N/A'):
          data_file.append(dictionary)


keys = ['EMPID', 'Common Name', 'PAYMENT_TYPE_CODE', 'CURRENCY', 'AMOUNT', 'EFFECTIVE_DATE',
        'COMMENT', 'PAYMENT_FROM_DATE', 'PAYMENT_TO_DATE', 'IS_GROSS', 'HIDDEN_UNTIL', 'PAID MANUALLY', 'PAYMENT_DATE']

bulk_upload = open('EMEA Bulk Upload.csv', 'wb')
dict_writer = csv.DictWriter(bulk_upload, keys, restval='', extrasaction='ignore')
dict_writer.writer.writerow(keys)
dict_writer.writerows(data_file)

print "Everything saved! Look up EMEA Bulk Upload.csv"

2 个答案:

答案 0 :(得分:1)

这是你的问题:

open_file = csv.DictReader(open(travel))
generators.append(open_file)    

travel_generators = generators[:]
massage_generators = generators[:]

您的travel_generatorsmassage_generators列表使用相同的打开文件进行迭代并获取数据;问题是,一旦文件被csv.DictReader中的massage_generators迭代了一次,它就已经用尽了,当您尝试在travel_generators中再次读取它时,您将不再获得更多数据。

请改为尝试:

generators.append(travel)     # just the filename    

travel_generators = generators[:]
massage_generators = generators[:]

然后在你的循环中:

for filename in massage_generators:
    for dictionary in csv.DictReader(open(filename)):

此外,在您收集文件名的第一个循环中:请注意循环仅保存匹配的 last 文件名。如果这是您的意图,您可以在break之后的行上添加travel = filename来明确说明。

答案 1 :(得分:0)

在您提供的示例中,generatorstravel_generatorsmassage_generators都是相同的,每个都包含open_file的相同实例。当您迭代massage_generators时,您将到达文件的末尾。然后,当您迭代travel_generators时,没有任何内容可以在open_file中进行迭代。

至少,您可以更改以下行:

open_file = csv.DictReader(open(travel))

要:

open_file = map(None, csv.DictReader(open(travel)))

open_file现在是一个列表,而不是一个生成器,可以迭代和重复。

但是,我建议您遍历该文件一次,并构建两个字典,并在每次迭代中写入DictWriter实例。

open_file = csv.DictReader(open(travel))
...
dict_writer = csv.DictWriter(bulk_upload, keys, restval='', extrasaction='ignore')
dict_writer.writer.writerow(keys)

for line in open_file:
    massage_dictionary={}
    massage_dictionary.update(line)
    ... # manipulate massage_dictionary
    dict_writer.writerow(massage_dictionary)

    travel_dictionary={}
    travel_dictionary.update(line)
    ... # manipulate travel_dictionary
    dict_writer.writerow(travel_dictionary)