Hướng dẫn convert ordereddict to string python - chuyển đổi đã đặt hàng thành chuỗi python

Tôi có một db postgres trong đó OrderedDict đã được lưu dưới dạng chuỗi. Tôi cần chuyển đổi chuỗi này thành json/dict để nó có thể được lưu trong một jsonfield. Làm thế nào tôi có thể chuyển đổi chuỗi này thành dict?

Ví dụ về chuỗi -

OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])

Tôi đã thử json.loads(string) nhưng nó gây ra lỗi giải mã. Bất kỳ giải pháp ngoài việc phân tích thủ công chuỗi?

Đã hỏi ngày 10 tháng 5 năm 2019 lúc 6:33May 10, 2019 at 6:33

Hướng dẫn convert ordereddict to string python - chuyển đổi đã đặt hàng thành chuỗi python

3

Bạn có thể sử dụng

from collections import OrderedDict
import json

x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"

#run string through eval and convert to dict
dct = dict(eval(x))
print(dct)
0 cho mục đích này.

from collections import OrderedDict
import json

x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"

#run string through eval and convert to dict
dct = dict(eval(x))
print(dct)

Đầu ra sẽ là

{'order_id': 'xxxxxx', 'tracking_id': 'xxxxxx', 
'bank_ref_no': 'xxxxx', 'order_status': 'Success'}

Đã trả lời ngày 10 tháng 5 năm 2019 lúc 6:41May 10, 2019 at 6:41

Hướng dẫn convert ordereddict to string python - chuyển đổi đã đặt hàng thành chuỗi python

Tôi biết bạn đã đề cập rằng bạn muốn một giải pháp mà không cần phân tích cú pháp thực tế, nhưng tùy chọn phân tích cú pháp cũng có thể khá đơn giản:

import ast

a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"

# get the inner list representation
a = a.replace("OrderedDict(", '')
a = a[:-1]

# convert to a list of tuples
x = ast.literal_eval(a)

dict(x)

Đã trả lời ngày 10 tháng 5 năm 2019 lúc 6:42May 10, 2019 at 6:42

Razdirazdirazdi

1.33014 Huy hiệu bạc18 Huy hiệu đồng14 silver badges18 bronze badges

Một cách tiếp cận khác là sử dụng Regex để trích xuất danh sách và sau đó sử dụng mô -đun

from collections import OrderedDict
import json

x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"

#run string through eval and convert to dict
dct = dict(eval(x))
print(dct)
1.

Ex:

import re
import ast
from collections import OrderedDict

s = """OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"""

print(OrderedDict(ast.literal_eval(re.search(r"(?<=OrderedDict\()(.*)\)$", s).group(1))))

Output:

OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])

Đã trả lời ngày 10 tháng 5 năm 2019 lúc 6:51May 10, 2019 at 6:51

Hướng dẫn convert ordereddict to string python - chuyển đổi đã đặt hàng thành chuỗi python

RakeshrakeshRakesh

79,8K17 Huy hiệu vàng72 Huy hiệu bạc109 Huy hiệu đồng17 gold badges72 silver badges109 bronze badges

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Từ điển là một container quan trọng và được sử dụng gần như trong mọi mã lập trình hàng ngày cũng như phát triển web. Nó càng được sử dụng, càng nhiều là yêu cầu để thành thạo nó và do đó cần phải tìm hiểu về chúng. & Nbsp; chúng ta hãy xem các cách khác nhau để thay đổi từ điển thành một chuỗi. .dumps () là một hàm sẵn trong thư viện JSON. Nó có lợi thế so với Pickle vì nó có hỗ trợ đa nền tảng. & NBSP;
    Let’s see the different ways of changing a dictionary into a string.
    Methods #1: Using json.dumps() 
    json.dumps() is an inbuilt function in json library. It has advantage over pickle because it has cross-platform support.
     

    Python3

    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    2
    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    3

    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    4
    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    5
    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    6
    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    7
    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    8
    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    9
    {'order_id': 'xxxxxx', 'tracking_id': 'xxxxxx', 
    'bank_ref_no': 'xxxxx', 'order_status': 'Success'}
    
    0

    {'order_id': 'xxxxxx', 'tracking_id': 'xxxxxx', 
    'bank_ref_no': 'xxxxx', 'order_status': 'Success'}
    
    1
    {'order_id': 'xxxxxx', 'tracking_id': 'xxxxxx', 
    'bank_ref_no': 'xxxxx', 'order_status': 'Success'}
    
    2
    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    8
    {'order_id': 'xxxxxx', 'tracking_id': 'xxxxxx', 
    'bank_ref_no': 'xxxxx', 'order_status': 'Success'}
    
    4
    {'order_id': 'xxxxxx', 'tracking_id': 'xxxxxx', 
    'bank_ref_no': 'xxxxx', 'order_status': 'Success'}
    
    0

    {'order_id': 'xxxxxx', 'tracking_id': 'xxxxxx', 
    'bank_ref_no': 'xxxxx', 'order_status': 'Success'}
    
    1
    {'order_id': 'xxxxxx', 'tracking_id': 'xxxxxx', 
    'bank_ref_no': 'xxxxx', 'order_status': 'Success'}
    
    7
    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    8
    {'order_id': 'xxxxxx', 'tracking_id': 'xxxxxx', 
    'bank_ref_no': 'xxxxx', 'order_status': 'Success'}
    
    9
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    0

    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    1
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    2
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    3
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    4

    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    1
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    2
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    7
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    8

    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    9
    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    5
    import re
    import ast
    from collections import OrderedDict
    
    s = """OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"""
    
    print(OrderedDict(ast.literal_eval(re.search(r"(?<=OrderedDict\()(.*)\)$", s).group(1))))
    
    1

    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    1
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    2
    import re
    import ast
    from collections import OrderedDict
    
    s = """OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"""
    
    print(OrderedDict(ast.literal_eval(re.search(r"(?<=OrderedDict\()(.*)\)$", s).group(1))))
    
    4
    import re
    import ast
    from collections import OrderedDict
    
    s = """OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"""
    
    print(OrderedDict(ast.literal_eval(re.search(r"(?<=OrderedDict\()(.*)\)$", s).group(1))))
    
    5
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    3
    import re
    import ast
    from collections import OrderedDict
    
    s = """OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"""
    
    print(OrderedDict(ast.literal_eval(re.search(r"(?<=OrderedDict\()(.*)\)$", s).group(1))))
    
    7

    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    1
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    2
    OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])
    
    0
    OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])
    
    1

    Output:

     
    initial dictionary = {‘testname’: ‘akshat’, ‘test2name’: ‘manjeet’, ‘test3name’: ‘nikhil’}
     
    final string = {“testname”: “akshat”, “test2name”: “manjeet”, “test3name”: “nikhil”} 

    & nbsp; & nbsp; Phương thức #2: Sử dụng str () & nbsp; hàm str () chuyển đổi giá trị được chỉ định thành một chuỗi. & nbsp; & nbsp;
    Methods #2: Using str() 
    The str() function converts the specified value into a string. 
     

    Python3

    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    4
    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    5
    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    6
    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    7
    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    8
    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    9
    {'order_id': 'xxxxxx', 'tracking_id': 'xxxxxx', 
    'bank_ref_no': 'xxxxx', 'order_status': 'Success'}
    
    0

    {'order_id': 'xxxxxx', 'tracking_id': 'xxxxxx', 
    'bank_ref_no': 'xxxxx', 'order_status': 'Success'}
    
    1
    {'order_id': 'xxxxxx', 'tracking_id': 'xxxxxx', 
    'bank_ref_no': 'xxxxx', 'order_status': 'Success'}
    
    2
    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    8
    {'order_id': 'xxxxxx', 'tracking_id': 'xxxxxx', 
    'bank_ref_no': 'xxxxx', 'order_status': 'Success'}
    
    4
    {'order_id': 'xxxxxx', 'tracking_id': 'xxxxxx', 
    'bank_ref_no': 'xxxxx', 'order_status': 'Success'}
    
    0

    {'order_id': 'xxxxxx', 'tracking_id': 'xxxxxx', 
    'bank_ref_no': 'xxxxx', 'order_status': 'Success'}
    
    1
    {'order_id': 'xxxxxx', 'tracking_id': 'xxxxxx', 
    'bank_ref_no': 'xxxxx', 'order_status': 'Success'}
    
    7
    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    8
    {'order_id': 'xxxxxx', 'tracking_id': 'xxxxxx', 
    'bank_ref_no': 'xxxxx', 'order_status': 'Success'}
    
    9
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    0

    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    1
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    2
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    3
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    4

    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    1
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    2
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    7
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    8

    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    9
    from collections import OrderedDict
    import json
    
    x = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    #run string through eval and convert to dict
    dct = dict(eval(x))
    print(dct)
    
    5
    import re
    import ast
    from collections import OrderedDict
    
    s = """OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"""
    
    print(OrderedDict(ast.literal_eval(re.search(r"(?<=OrderedDict\()(.*)\)$", s).group(1))))
    
    1

    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    1
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    2
    import re
    import ast
    from collections import OrderedDict
    
    s = """OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"""
    
    print(OrderedDict(ast.literal_eval(re.search(r"(?<=OrderedDict\()(.*)\)$", s).group(1))))
    
    4
    import re
    import ast
    from collections import OrderedDict
    
    s = """OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"""
    
    print(OrderedDict(ast.literal_eval(re.search(r"(?<=OrderedDict\()(.*)\)$", s).group(1))))
    
    5
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    3
    import re
    import ast
    from collections import OrderedDict
    
    s = """OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"""
    
    print(OrderedDict(ast.literal_eval(re.search(r"(?<=OrderedDict\()(.*)\)$", s).group(1))))
    
    7

    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    1
    import ast
    
    a = "OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])"
    
    # get the inner list representation
    a = a.replace("OrderedDict(", '')
    a = a[:-1]
    
    # convert to a list of tuples
    x = ast.literal_eval(a)
    
    dict(x)
    
    2
    OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])
    
    0
    OrderedDict([('order_id', 'xxxxxx'), ('tracking_id', 'xxxxxx'), ('bank_ref_no', 'xxxxx'), ('order_status', 'Success')])
    
    1

    Output:

     
    initial dictionary = {‘test2name’: ‘manjeet’, ‘testname’: ‘akshat’, ‘test3name’: ‘nikhil’}
     
    final string = {‘test2name’: ‘manjeet’, ‘testname’: ‘akshat’, ‘test3name’: ‘nikhil’} 

    Làm thế nào để bạn chuyển đổi một đối tượng thành một chuỗi trong Python?

    Chuyển đổi đối tượng thành chuỗi mọi thứ là một đối tượng trong Python. Vì vậy, tất cả các đối tượng tích hợp có thể được chuyển đổi thành các chuỗi bằng các phương thức str () và repr ().using the str() and repr() methods.

    Làm cách nào để chuyển đổi danh sách thành một chuỗi trong Python?

    Để chuyển đổi danh sách thành một chuỗi, hãy sử dụng khả năng hiểu danh sách Python và hàm tham gia (). Sự hiểu biết danh sách sẽ đi qua từng phần tử một và phương thức tham gia () sẽ kết hợp các phần tử của danh sách thành một chuỗi mới và trả về nó làm đầu ra.use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.

    Đặt hàng có nghĩa là gì trong Python?

    Lệnh của Python là một lớp con Dict bảo tồn thứ tự trong đó các cặp giá trị khóa, thường được gọi là các mục, được đưa vào từ điển.Khi bạn lặp lại một đối tượng đặt hàng, các mục được đi qua theo thứ tự ban đầu.Nếu bạn cập nhật giá trị của một khóa hiện có, thì thứ tự vẫn không thay đổi.a dict subclass that preserves the order in which key-value pairs, commonly known as items, are inserted into the dictionary. When you iterate over an OrderedDict object, items are traversed in the original order. If you update the value of an existing key, then the order remains unchanged.

    Làm thế nào để tôi thay đổi một đơn đặt hàng thành một từ điển?

    Vì vậy, để chuyển đổi trật tự lồng nhau thành Dict, chúng tôi đang sử dụng JSON ...
    Hình thức đầy đủ của JSON là ký hiệu đối tượng JavaScript.Điều đó có nghĩa là một tệp tập lệnh (thực thi) được tạo bằng văn bản bằng ngôn ngữ lập trình, được sử dụng để lưu trữ và chuyển dữ liệu.....
    JSON.....
    Json ..