gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:26:43 25 4
gpt4 key购买 nike

我使用 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"

最佳答案

这是你的问题:

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

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

您的 travel_generatorsmassage_generators 列表使用相同的打开文件来迭代并获取数据;问题是,一旦文件被 massage_generators 中的 csv.DictReader 迭代一次,它就已经耗尽,当您尝试读取它时,您将无法获得更多数据再次在 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)):

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

关于python - 如何将一个词典列表添加到另一个词典列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7875051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com