Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中从一个字典提取一个键到另一个字典_Python_Dictionary - Fatal编程技术网

在Python中从一个字典提取一个键到另一个字典

在Python中从一个字典提取一个键到另一个字典,python,dictionary,Python,Dictionary,我有一本大python字典。它的一个键有另一个字典作为值。我想使用这些值创建一个新字典,然后从原始字典中删除该键 是否有任何函数可以将值导出到另一个字典中?要删除,我知道我可以使用.pop()函数。我尝试过谷歌搜索,但没有成功 这是字典。我用星星来屏蔽敏感信息。我需要的关键是账单地址,它有另一个字典作为值: { 'shipping_cost_tax': '0.0000', 'refunded_amount': '0.0000', 'external_source': No

我有一本大python字典。它的一个键有另一个字典作为值。我想使用这些值创建一个新字典,然后从原始字典中删除该键

是否有任何函数可以将值导出到另一个字典中?要删除,我知道我可以使用
.pop()
函数。我尝试过谷歌搜索,但没有成功

这是字典。我用星星来屏蔽敏感信息。我需要的关键是
账单地址
,它有另一个字典作为值:

{
    'shipping_cost_tax': '0.0000',
    'refunded_amount': '0.0000',
    'external_source': None,
    'discount_amount': '0.0000',
    'base_wrapping_cost': '0.0000',
    'shipping_cost_tax_class_id': 2,
    'payment_method': 'PayPal',
    'handling_cost_ex_tax': '0.0000',
    'store_credit_amount': '0.0000',
    'shipping_cost_inc_tax': '11.0000',
    'handling_cost_tax_class_id': 2,
    'currency_id': 1,
    'payment_status': 'captured',
    'subtotal_ex_tax': '99.0000',
    'total_inc_tax': '11.0000',
    'handling_cost_inc_tax': '0.0000',
    'total_ex_tax': '11.0000',
    'is_deleted': False,
    'status_id': 5,
    'id': 614534,
    'shipping_cost_ex_tax': '11.0000',
    'date_shipped': '',
    'order_source': 'www',
    'status': 'Cancelled',
    'handling_cost_tax': '0.0000',
    'items_total': 3,
    'wrapping_cost_tax': '0.0000',
    'date_created': 'Wed,
    09 Jul 2014 12:22:17 +0000',
    'total_tax': '0.0000',
    'order_is_digital': False,
    'date_modified': 'Thu,
    30 Oct 2014 02:34:07 +0000',
    'geoip_country': 'Australia',
    'base_shipping_cost': '11.0000',
    'payment_provider_id': '**************',
    'staff_notes': '',
    'default_currency_id': 1,
    'currency_code': 'AUD',
    'currency_exchange_rate': '1.0000000000',
    'coupon_discount': '99.0000',
    'customer_message': '',
    'subtotal_inc_tax': '99.0000',
    'gift_certificate_amount': '0.0000',
    'items_shipped': 0,
    'default_currency_code': 'AUD',
    'customer_id': 1,
    'geoip_country_iso2': 'AU',
    'ip_address': '124.168.160.136',
    'shipping_address_count': 1,
    'wrapping_cost_ex_tax': '0.0000',
    'base_handling_cost': '0.0000',
    'wrapping_cost_tax_class_id': 3,
    'ebay_order_id': '0',
    'wrapping_cost_inc_tax': '0.0000',
    'billing_address': {
        'state': '*******',
        'street_1': '*************',
        'street_2': '',
        'country_iso2': 'AU',
        'last_name': '************',
        'company': '***************',
        'country': 'Australia',
        'first_name': '*********',
        'email': '***************',
        'phone': '*************',
        'city': '*************',
        'zip': '************'
    },
    'subtotal_tax': '0.0000'
}
编辑:

allOrdersData是一个文件,我有很多字典,就像我之前发布的一样,每行一个。当我尝试运行它时,出现以下错误:

TypeError:字符串索引必须是整数

编辑2:

没关系,让它与ast.literal\u eval一起工作:

import ast


def popAndMergeDicts(line):

    dictLine = ast.literal_eval(line)
    tempDict = dictLine['billing_address']
    del dictLine['billing_address']
    for i in tempDict:
        dictLine[i] = tempDict[i]
    print(dictLine)


def process_file(filename):
    lines = tuple(open(filename))
    for line in lines[0:]:
        popAndMergeDicts(line)

process_file('allOrdersData')
如果您只想引用“子词典”,您可以简单地使用(其中
d
是您的原始词典):

如果您想要一份不同的单独副本,可以使用以下任何一种:

billing2 = dict(d['billing_address'])
billing3 = d['billing_address'].copy()
billing4 = copy.copy(d['billing_address'])  # after import copy
billing1
中值的更改将反映在原始词典中。对其他三个值的更改不会反映在原始词典中

如果您还想删除
账单地址
键(就像您在问题中建议的那样,但在评论中要后退),则首先使用上述方法之一,然后使用:

del d['billing_address']
或者,作为一个步骤,如果您承诺从原始字典中删除密钥,请使用

如果您只想引用“子词典”,您可以简单地使用(其中
d
是您的原始词典):

如果您想要一份不同的单独副本,可以使用以下任何一种:

billing2 = dict(d['billing_address'])
billing3 = d['billing_address'].copy()
billing4 = copy.copy(d['billing_address'])  # after import copy
billing1
中值的更改将反映在原始词典中。对其他三个值的更改不会反映在原始词典中

如果您还想删除
账单地址
键(就像您在问题中建议的那样,但在评论中要后退),则首先使用上述方法之一,然后使用:

del d['billing_address']
或者,作为一个步骤,如果您承诺从原始字典中删除密钥,请使用

如果您只想引用“子词典”,您可以简单地使用(其中
d
是您的原始词典):

如果您想要一份不同的单独副本,可以使用以下任何一种:

billing2 = dict(d['billing_address'])
billing3 = d['billing_address'].copy()
billing4 = copy.copy(d['billing_address'])  # after import copy
billing1
中值的更改将反映在原始词典中。对其他三个值的更改不会反映在原始词典中

如果您还想删除
账单地址
键(就像您在问题中建议的那样,但在评论中要后退),则首先使用上述方法之一,然后使用:

del d['billing_address']
或者,作为一个步骤,如果您承诺从原始字典中删除密钥,请使用

如果您只想引用“子词典”,您可以简单地使用(其中
d
是您的原始词典):

如果您想要一份不同的单独副本,可以使用以下任何一种:

billing2 = dict(d['billing_address'])
billing3 = d['billing_address'].copy()
billing4 = copy.copy(d['billing_address'])  # after import copy
billing1
中值的更改将反映在原始词典中。对其他三个值的更改不会反映在原始词典中

如果您还想删除
账单地址
键(就像您在问题中建议的那样,但在评论中要后退),则首先使用上述方法之一,然后使用:

del d['billing_address']
或者,作为一个步骤,如果您承诺从原始字典中删除密钥,请使用

只需使用
.pop()
:它返回它弹出的值。例如

#!/usr/bin/env python

import pprint

big_dict = {
    'shipping_cost_tax': '0.0000',
    'refunded_amount': '0.0000',
    #etc
    'billing_address': {
        'state': '*******',
        'street_1': '*************',
        'street_2': '',
        'country_iso2': 'AU',
        #etc
    },
    'subtotal_tax': '0.0000'
}

print 'Before'
pprint.pprint(big_dict, indent=4)

bill_dict = big_dict.pop('billing_address')

print '\nBill dict'
pprint.pprint(bill_dict, indent=4)

print '\nAfter'
pprint.pprint(big_dict, indent=4)
输出

Before
{   'billing_address': {   'country_iso2': 'AU',
                           'state': '*******',
                           'street_1': '*************',
                           'street_2': ''},
    'refunded_amount': '0.0000',
    'shipping_cost_tax': '0.0000',
    'subtotal_tax': '0.0000'}

Bill dict
{   'country_iso2': 'AU',
    'state': '*******',
    'street_1': '*************',
    'street_2': ''}

After
{   'refunded_amount': '0.0000',
    'shipping_cost_tax': '0.0000',
    'subtotal_tax': '0.0000'}

要将键/值保留在原始字典中,而不是创建新字典,您可以按照Marichyasana的建议执行:

bill_dict = big_dict.pop('billing_address')
for k in bill_dict:
    big_dict[k] = bill_dict[k]
del bill_dict

print '\nAfter'
pprint.pprint(big_dict, indent=4)
输出

After
{   'country_iso2': 'AU',
    'refunded_amount': '0.0000',
    'shipping_cost_tax': '0.0000',
    'state': '*******',
    'street_1': '*************',
    'street_2': '',
    'subtotal_tax': '0.0000'}
我还删除了临时的
账单。这并不是绝对必要的,因为一旦超出范围,
账单将自动删除。

只需使用
.pop()
:它返回它弹出的值。例如

#!/usr/bin/env python

import pprint

big_dict = {
    'shipping_cost_tax': '0.0000',
    'refunded_amount': '0.0000',
    #etc
    'billing_address': {
        'state': '*******',
        'street_1': '*************',
        'street_2': '',
        'country_iso2': 'AU',
        #etc
    },
    'subtotal_tax': '0.0000'
}

print 'Before'
pprint.pprint(big_dict, indent=4)

bill_dict = big_dict.pop('billing_address')

print '\nBill dict'
pprint.pprint(bill_dict, indent=4)

print '\nAfter'
pprint.pprint(big_dict, indent=4)
输出

Before
{   'billing_address': {   'country_iso2': 'AU',
                           'state': '*******',
                           'street_1': '*************',
                           'street_2': ''},
    'refunded_amount': '0.0000',
    'shipping_cost_tax': '0.0000',
    'subtotal_tax': '0.0000'}

Bill dict
{   'country_iso2': 'AU',
    'state': '*******',
    'street_1': '*************',
    'street_2': ''}

After
{   'refunded_amount': '0.0000',
    'shipping_cost_tax': '0.0000',
    'subtotal_tax': '0.0000'}

要将键/值保留在原始字典中,而不是创建新字典,您可以按照Marichyasana的建议执行:

bill_dict = big_dict.pop('billing_address')
for k in bill_dict:
    big_dict[k] = bill_dict[k]
del bill_dict

print '\nAfter'
pprint.pprint(big_dict, indent=4)
输出

After
{   'country_iso2': 'AU',
    'refunded_amount': '0.0000',
    'shipping_cost_tax': '0.0000',
    'state': '*******',
    'street_1': '*************',
    'street_2': '',
    'subtotal_tax': '0.0000'}
我还删除了临时的
账单。这并不是绝对必要的,因为一旦超出范围,
账单将自动删除。

只需使用
.pop()
:它返回它弹出的值。例如

#!/usr/bin/env python

import pprint

big_dict = {
    'shipping_cost_tax': '0.0000',
    'refunded_amount': '0.0000',
    #etc
    'billing_address': {
        'state': '*******',
        'street_1': '*************',
        'street_2': '',
        'country_iso2': 'AU',
        #etc
    },
    'subtotal_tax': '0.0000'
}

print 'Before'
pprint.pprint(big_dict, indent=4)

bill_dict = big_dict.pop('billing_address')

print '\nBill dict'
pprint.pprint(bill_dict, indent=4)

print '\nAfter'
pprint.pprint(big_dict, indent=4)
输出

Before
{   'billing_address': {   'country_iso2': 'AU',
                           'state': '*******',
                           'street_1': '*************',
                           'street_2': ''},
    'refunded_amount': '0.0000',
    'shipping_cost_tax': '0.0000',
    'subtotal_tax': '0.0000'}

Bill dict
{   'country_iso2': 'AU',
    'state': '*******',
    'street_1': '*************',
    'street_2': ''}

After
{   'refunded_amount': '0.0000',
    'shipping_cost_tax': '0.0000',
    'subtotal_tax': '0.0000'}

要将键/值保留在原始字典中,而不是创建新字典,您可以按照Marichyasana的建议执行:

bill_dict = big_dict.pop('billing_address')
for k in bill_dict:
    big_dict[k] = bill_dict[k]
del bill_dict

print '\nAfter'
pprint.pprint(big_dict, indent=4)
输出

After
{   'country_iso2': 'AU',
    'refunded_amount': '0.0000',
    'shipping_cost_tax': '0.0000',
    'state': '*******',
    'street_1': '*************',
    'street_2': '',
    'subtotal_tax': '0.0000'}
我还删除了临时的
账单。这并不是绝对必要的,因为一旦超出范围,
账单将自动删除。

只需使用
.pop()
:它返回它弹出的值。例如

#!/usr/bin/env python

import pprint

big_dict = {
    'shipping_cost_tax': '0.0000',
    'refunded_amount': '0.0000',
    #etc
    'billing_address': {
        'state': '*******',
        'street_1': '*************',
        'street_2': '',
        'country_iso2': 'AU',
        #etc
    },
    'subtotal_tax': '0.0000'
}

print 'Before'
pprint.pprint(big_dict, indent=4)

bill_dict = big_dict.pop('billing_address')

print '\nBill dict'
pprint.pprint(bill_dict, indent=4)

print '\nAfter'
pprint.pprint(big_dict, indent=4)
输出

Before
{   'billing_address': {   'country_iso2': 'AU',
                           'state': '*******',
                           'street_1': '*************',
                           'street_2': ''},
    'refunded_amount': '0.0000',
    'shipping_cost_tax': '0.0000',
    'subtotal_tax': '0.0000'}

Bill dict
{   'country_iso2': 'AU',
    'state': '*******',
    'street_1': '*************',
    'street_2': ''}

After
{   'refunded_amount': '0.0000',
    'shipping_cost_tax': '0.0000',
    'subtotal_tax': '0.0000'}

要将键/值保留在原始字典中,而不是创建新字典,您可以按照Marichyasana的建议执行:

bill_dict = big_dict.pop('billing_address')
for k in bill_dict:
    big_dict[k] = bill_dict[k]
del bill_dict

print '\nAfter'
pprint.pprint(big_dict, indent=4)
输出

After
{   'country_iso2': 'AU',
    'refunded_amount': '0.0000',
    'shipping_cost_tax': '0.0000',
    'state': '*******',
    'street_1': '*************',
    'street_2': '',
    'subtotal_tax': '0.0000'}

我还删除了临时的
账单。这并不是绝对必要的,因为一旦超出范围,
bill\u dict
将自动删除。

让“a”作为词典的名称
让“b”作为帐单地址字典的名称

b=a['billing_address']
del a['billing_address']
for i in b:
    a[i]=b[i]

让“a”作为词典的名称
让“b”作为帐单地址字典的名称

b=a['billing_address']
del a['billing_address']
for i in b:
    a[i]=b[i]

让“a”作为词典的名称
让“b”作为帐单地址字典的名称

b=a['billing_address']
del a['billing_address']
for i in b:
    a[i]=b[i]

让“a”作为词典的名称
让“b”作为帐单地址字典的名称

b=a['billing_address']
del a['billing_address']
for i in b:
    a[i]=b[i]

没有任何样品吗