I have this dictionary named element, and a list of dictionaries named l1
l1 = [{"parent_id": "",
"productid": "6x2562",
"sequence_no": "1",
"quantity": "8.000",
"listprice": "",
"discount_percent": "",
"discount_amount": "",
"comment": "new list item 1",
"description": "some text",
"lineitem_id": "1",
"tax4": "",
"id": ""},
{"parent_id": "",
"productid": "6x2562",
"sequence_no": "1",
"quantity": "8.000",
"listprice": "",
"discount_percent": "",
"discount_amount": "",
"comment": "new list item 1",
"description": "some text",
"lineitem_id": "1",
"tax4": "",
"id": ""}]
element = {
"subject": "testtest ",
"potential_id": "",
"quotestage": "Created",
"validtill": "2023-03-11 10:48:10.339414",
"contact_id": "4x540",
"id": "",
"LineItems": []
}
element dictionary contains an empty list as a value to a key named LineItems. I am inserting a list of dictionaries (l1) in place of this empty list using following code:
element['LineItems'].append(l1)
print(element)
The problem I am having is that I am getting the following result which contains two [ ] brackets rather then one.
My current output:
{'subject': 'testtest ', 'potential_id': '', 'quotestage': 'Created', 'validtill': '2023-03-11 10:48:10.339414', 'contact_id': '4x540', 'id': '', 'LineItems': [[{'parent_id': '', 'productid': '6x2562', 'sequence_no': '1', 'quantity': '8.000', 'listprice': '', 'discount_percent': '', 'discount_amount': '', 'comment': 'new list item 1', 'description': 'some text', 'lineitem_id': '1', 'tax4': '', 'id': ''}, {'parent_id': '', 'productid': '6x2562', 'sequence_no': '1', 'quantity': '8.000', 'listprice': '', 'discount_percent': '', 'discount_amount': '', 'comment': 'new list item 1', 'description': 'some text', 'lineitem_id': '1', 'tax4': '', 'id': ''}]]}
Expected Output:
{'subject': 'testtest ', 'potential_id': '', 'quotestage': 'Created', 'validtill': '2023-03-11 10:48:10.339414', 'contact_id': '4x540', 'id': '', 'LineItems': [{'parent_id': '', 'productid': '6x2562', 'sequence_no': '1', 'quantity': '8.000', 'listprice': '', 'discount_percent': '', 'discount_amount': '', 'comment': 'new list item 1', 'description': 'some text', 'lineitem_id': '1', 'tax4': '', 'id': ''}, {'parent_id': '', 'productid': '6x2562', 'sequence_no': '1', 'quantity': '8.000', 'listprice': '', 'discount_percent': '', 'discount_amount': '', 'comment': 'new list item 1', 'description': 'some text', 'lineitem_id': '1', 'tax4': '', 'id': ''}]}
I have no idea how to do this as I have been searching for whole day now.
Any help will be appreciated.
Thanks in advance!
i have alot of this line
('235753', 'BayEnesYT', '$2y$12$laiU7F7HWJoXuryMTmgb6uKDfiOcxqD/R6Mxjg.KVNn2TK/Ra2Vwq', 'BNnuyZNL', 'zb6WPCvWYDwQmwZJQI7sypkc6oqVjZpSvnlg8gYYztJm6JYmJh', 'm.enesberber2009#gmail.com', '0', '0', '', '', '', '2', '', '0', '', '1560100802', '1560689379', '1560102670', '0', '', '0', '', '', '', '', '', 'all', '', '1', '0', '0', '0', '1', '0', '1', '0', '1', '0', 'linear', '1', '1', '1', '1', '1', '1', '0', '0', '0', '', '', '', '0', '0', '', '', '0', '0', '0', '0', '', '', '', '0', '0', '0', 0x51D69F63, 0x5567522E, '', '1894', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '0', '', '0', '0.00', '1', '0', '0', '0', '', '1', '1', '2', '0', '0', '0', '0', '[]', '1', null, null, null, '1', '', '', '0', '0', '0', '', '', '', '', '', '0', '', '', '', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '', '0', '', '0', '0', '0', '', '', '', '', '', '', '', '', '', '', '0', '', 'mybb_bcrypt', '0', '');
and i need to remove text before the 5th comma and after the 6th comma
so the result would be m.enesberber2009#gmail.com only
i want to use regex replacement method with notepad++
Replace this expression
^\(?(?:'[^']*',\s*){5}('[^']+').+
with the first captured group, see a demo on regex101.com.
I had help on this matter pairing IP Addresses from this site which worked. I tried to amend the same script to use to pair Port numbers but keep getting a float attribute error. I have tried changing it but as a newbie I have failed to make any headway please help.
This is the data
['', '', '', '', '', '', '', 'Pool Member Port', '', 10001.0, 10001.0, '', '', '', '', '', '', '', 11001.0, 11001.0, '', '', '', '', '', '', '', 12001.0, 12001.0, '', '', '', '', '', '', '', 14001.0, 14001.0, '', '', '', '', '', '', '', 14001.0, 14001.0, '', '', '', '', '', '', '', 14001.0, 14001.0, '', '', '', '', '', '', '', 14001.0, 14001.0, '', '', '', '', '', '', '', 14001.0, '', 10001.0, '', '', '', '', '', '', '', '', 11001.0, '', '', '', '', '', '', '', '', 12001.0, '', '', '', '', '', '', '', '', 14001.0, '', '', '', '', '', '', '', '', 22.0, '', '', '', '', '', '', '', '', 22.0, '', 22.0, '', '', '', '', '', '', '', '', 14001.0, '', '', '', '', '', '', '', '', '', '', '', 'Receive string', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
This is my amended script
Pool_Ports = [[]]
for x in PPoData[PPoData.index('Pool Member Port'):]:
if not x:
if Pool_Ports[-1]:
Pool_Ports.append([])
else:
#Pool_Ports[-1].append(x.partition(' ')[0])
print(Pool_Ports)
error message
AttributeError: 'float' object has no attribute 'partition'
Previous link which works for IP addresses
Pair IP addresses in a list pulled from xlrd
The error is simple, really. Your input list has it's ports values (e.g. 11001.0) stored as floats, therefore the code breaks. The instant fix would be casting x to string right before the partition.
Pool_Ports[-1].append(str(x).partition(' ')[0])
However, the output ends up being
[['10001.0', '10001.0'], ['11001.0', '11001.0'], ['12001.0',
'12001.0'], ['14001.0', '14001.0'], ['14001.0', '14001.0'],
['14001.0', '14001.0'], ['14001.0', '14001.0'], ['14001.0'],
['10001.0'], ['11001.0'], ['12001.0'], ['14001.0'], ['22.0'],
['22.0'], ['22.0'], ['14001.0'], ['Receive'], []]
Which is not what you need, beacuse it has ['Receive'] and [], which are not valid pool ports.
So, inspired by #cᴏʟᴅsᴘᴇᴇᴅ 's answer to the previous question, I would recommend you to use regex.
import re
PPoData = ['', '', '', '', '', '', '', 'Pool Member Port', '', 10001.0, 10001.0, '', '', '', '', '', '', '', 11001.0, 11001.0, '', '', '', '', '', '', '', 12001.0, 12001.0, '', '', '', '', '', '', '', 14001.0, 14001.0, '', '', '', '', '', '', '', 14001.0, 14001.0, '', '', '', '', '', '', '', 14001.0, 14001.0, '', '', '', '', '', '', '', 14001.0, 14001.0, '', '', '', '', '', '', '', 14001.0, '', 10001.0, '', '', '', '', '', '', '', '', 11001.0, '', '', '', '', '', '', '', '', 12001.0, '', '', '', '', '', '', '', '', 14001.0, '', '', '', '', '', '', '', '', 22.0, '', '', '', '', '', '', '', '', 22.0, '', 22.0, '', '', '', '', '', '', '', '', 14001.0, '', '', '', '', '', '', '', '', '', '', '', 'Receive string', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
pattern = '\d+\.\d+'
Pool_Ports = [[]]
for x in PPoData:
if not x and Pool_Ports[-1]:
Pool_Ports.append([])
m = re.match(pattern, str(x))
if m:
Pool_Ports[-1].append(str(x))
if [] in Pool_Ports:
Pool_Ports.remove([])
print(Pool_Ports)
# [['10001.0', '10001.0'], ['11001.0', '11001.0'], ['12001.0', '12001.0'], ['14001.0', '14001.0'], ['14001.0', '14001.0'], ['14001.0', '14001.0'], ['14001.0', '14001.0'], ['14001.0'], ['10001.0'], ['11001.0'], ['12001.0'], ['14001.0'], ['22.0'], ['22.0'], ['22.0'], ['14001.0']]