Converting dictionary into a list of dictionaries - python
So, I've been tasked with converting a string into a dict (has to be using regex). I've done a findall to separate each element but not sure how to put it together.
I have the following code:
import re
def edata():
with open("employeedata.txt", "r") as file:
employeedata = file.read()
IP_field = re.findall(r"\d+[.]\d+[.]\d+[.]\d+", employeedata)
username_field = re.findall (r"[a-z]+\d+|- -", employeedata)
date_field = re.findall (r"\d+\/[A-Z][a-z][0-9]+\/\d\d\d\d:\d+:\d+:\d+ -\d+", employeedata)
type_field = re.findall (r'"(.*)?"', employeedata)
Fields = ["IP","username","date","type"]
Fields2 = IP_field, username_field, date_field, type_field
dictionary = dict(zip(Fields,Fields2))
return dictionary
print(edata())
Current output:
{ "IP": ["190.912.120.151", "190.912.120.151"], "username": ["skynet10001", "skynet10001"] etc }
Expected output:
[{ "IP": "190.912.120.151", "username": "skynet10001" etc },
{ "IP": "190.912.120.151", "username": "skynet10001" etc }]
Another solution that uses the dictionary that you have already constructed. This code uses list comprehension and the zip function to produce a list of dictionaries from the existing dictionary variable.
import re
def edata():
with open("employeedata.txt", "r") as file:
employeedata = file.read()
IP_field = re.findall(r"\d+[.]\d+[.]\d+[.]\d+", employeedata)
username_field = re.findall (r"[a-z]+\d+|- -", employeedata)
date_field = re.findall (r"\[(.*?)\]", employeedata) ## changed your regex for the date field
type_field = re.findall (r'"(.*)?"', employeedata)
Fields = ["IP","username","date","type"]
Fields2 = IP_field, username_field, date_field, type_field
dictionary = dict(zip(Fields,Fields2))
result_dictionary = [dict(zip(dictionary, i)) for i in zip(*dictionary.values())] ## convert to list of dictionaries
return result_dictionary
print(edata())
You can use
import re
rx = re.compile(r'^(?P<IP>\d+(?:\.\d+){3})\s+\S+\s+(?P<Username>[a-z]+\d+)\s+\[(?P<Date>[^][]+)]\s+"(?P<Type>[^"]*)"')
def edata():
results = []
with open("downloads/employeedata.txt", "r") as file:
for line in file:
match = rx.search(line)
if match:
results.append(match.groupdict())
return results
print(edata())
See the online Python demo. For the file = ['190.912.120.151 - skynet10001 [19/Jan/2012] "Temp"', '221.143.119.260 - terminator002 [16/Feb/2021] "Temp 2"'] input, the output will be:
[{'IP': '190.912.120.151', 'Username': 'skynet10001', 'Date': '19/Jan/2012', 'Type': 'Temp'}, {'IP': '221.143.119.260', 'Username': 'terminator002', 'Date': '16/Feb/2021', 'Type': 'Temp 2'}]
The regex is
^(?P<IP>\d+(?:\.\d+){3})\s+\S+\s+(?P<Username>[a-z]+\d+)\s+\[(?P<Date>[^][]+)]\s+"(?P<Type>[^"]*)"
See the regex demo. Details:
^ - start of string
(?P<IP>\d+(?:\.\d+){3}) - Group "IP": one or more digits and then three occurrences of a . and one or more digits
\s+\S+\s+ - one or more non-whitespace chars enclosed with one or more whitespace chars on both ends
(?P<Username>[a-z]+\d+) - Group "Username": one or more lowercase ASCII letters and then one or more digits
\s+ - one or more whitespaces
\[ - a [ char
(?P<Date>[^][]+) - Group "Date": one or more chars other than [ and ]
]\s+" - a ] char, one or more whitespaces, "
(?P<Type>[^"]*) - Group "Type": zero or more chars other than "
" - a " char.
Related
list index out of range in list of lists Python
I have file of data and i want to convert it to list like this : example_dict = {"host":"146.204.224.152", "user_name":"feest6811", "time":"21/Jun/2019:15:45:24 -0700", "request":"POST /incentivize HTTP/1.1"} Example of one object of temp_list data: [{'host': '197.109.77.178', 'user_name': 'kertzmann3129', 'time': '[21/Jun/2019:15:45:25 -0700]', 'request': '"DELETE /virtual/solutions/target/web+services HTTP/2.0"'}] I tried to use this code but get error list index out of range : import re def logs(): with open("logdata.txt", "r") as file: logdata = file.read() return logdata pattern1 = re.compile(r"\n") pattern2 = re.compile(r"\s") Temp_List = [] Final_List = [] All_Data = pattern1.split(logs()) for item in All_Data: temp = pattern2.split(item) Temp_List = { "host": temp[0], "user_name": temp[2],"time":temp[3]+' '+temp[4],"request":temp[5]+' '+temp[6]+' '+temp[7]} Final_List.append(Temp_List) Final_List
As Patrick Artner pointed it out, when your code executes the following line, there is no guarantee that you have sufficient number of elements in your temp list: Temp_List = { "host": temp[0], "user_name": temp[2],"time":temp[3]+' '+temp[4],"request":temp[5]+' '+temp[6]+' '+temp[7]} To avoid this, you should check whether you have the required number of elements in the array by puting an if statement in the code, similar to the one below: if len(temp) < 8: Temp_List = { "host": temp[0], "user_name": temp[2],"time":temp[3]+' '+temp[4],"request":temp[5]+' '+temp[6]+' '+temp[7]} Other suggestion: You don't need the regex library to split a string like that. You can simply call my_string.split(r'\s')
Dictionary from a String with particular structure
I am using python 3 to read this file and convert it to a dictionary. I have this string from a file and I would like to know how could be possible to create a dictionary from it. [User] Date=10/26/2003 Time=09:01:01 AM User=teodor UserText=Max Cor UserTextUnicode=392039n9dj90j32 [System] Type=Absolute Dnumber=QS236 Software=1.1.1.2 BuildNr=0923875 Source=LAM Column=OWKD [Build] StageX=12345 Spotter=2 ApertureX=0.0098743 ApertureY=0.2431899 ShiftXYZ=-4.234809e-002 [Text] Text=Here is the Text files DataBaseNumber=The database number is 918723 ..... (There are more than 1000 lines per file) ... On the text I have "Name=Something" and then I would like to convert it as follows: {'Date':'10/26/2003', 'Time':'09:01:01 AM' 'User':'teodor' 'UserText':'Max Cor' 'UserTextUnicode':'392039n9dj90j32'.......} The word between [ ] can be removed, like [User], [System], [Build], [Text], etc... In some fields there is only the first part of the string: [Colors] Red= Blue= Yellow= DarkBlue=
What you have is an ordinary properties file. You can use this example to read the values into map: try (InputStream input = new FileInputStream("your_file_path")) { Properties prop = new Properties(); prop.load(input); // prop.getProperty("User") == "teodor" } catch (IOException ex) { ex.printStackTrace(); } EDIT: For Python solution, refer to the answerred question. You can use configparser to read .ini, or .properties files (format you have). import configparser config = configparser.ConfigParser() config.read('your_file_path') # config['User'] == {'Date': '10/26/2003', 'Time': '09:01:01 AM'...} # config['User']['User'] == 'teodor' # config['System'] == {'Type': 'Abosulte', ...}
Can easily be done in python. Assuming your file is named test.txt. This will also work for lines with nothing after the = as well as lines with multiple =. d = {} with open('test.txt', 'r') as f: for line in f: line = line.strip() # Remove any space or newline characters parts = line.split('=') # Split around the `=` if len(parts) > 1: d[parts[0]] = ''.join(parts[1:]) print(d) Output: { "Date": "10/26/2003", "Time": "09:01:01 AM", "User": "teodor", "UserText": "Max Cor", "UserTextUnicode": "392039n9dj90j32", "Type": "Absolute", "Dnumber": "QS236", "Software": "1.1.1.2", "BuildNr": "0923875", "Source": "LAM", "Column": "OWKD", "StageX": "12345", "Spotter": "2", "ApertureX": "0.0098743", "ApertureY": "0.2431899", "ShiftXYZ": "-4.234809e-002", "Text": "Here is the Text files", "DataBaseNumber": "The database number is 918723" }
I would suggest to do some cleaning to get rid of the [] lines. After that you can split those lines by the "=" separator and then convert it to a dictionary.
Extract array values from JSON with character removal in python
I have the following JSON string and I am trying to extract the values to a python list. I achieved getting the id_list string but I want to get every single value without the : in each of them. EDIT: Using python json library is not an option. My approach (never used a lot of regex before): https://regex101.com/r/qxYe9N/1 I want to use the expression with re.filterall(EXPR, jsonstr) to receive a list like: result = ["B01M8QSY16", "B017XBDBI6", ...more ] { "ajax": { "params": { "asinMetadataKeys": "adId", "featureId": "SimilaritiesCarousel", "reftagPrefix": "pd_sbs_60", "widgetTemplateClass": "PI::Similarities::ViewTemplates::Carousel::Desktop", "imageHeight": 160, "linkGetParameters": "{\"pf_rd_s\":\"desktop-dp-sims\",\"pf_rd_m\":\"A3JWKAKR8XB7XF\",\"pd_rd_r\":\"ac83cd73-b019-11e8-99c8-33d23753c678\",\"pf_rd_r\":\"H21WNBAW5EGZX90ND4PN\",\"pf_rd_t\":\"40701\",\"pd_rd_wg\":\"e6DPw\",\"pf_rd_p\":\"946762da-975a-438a-9e2b-a585cbe769b5\",\"pf_rd_i\":\"desktop-dp-sims\",\"pd_rd_w\":\"xg8TH\"}", "faceoutTemplateClass": "PI::P13N::ViewTemplates::Product::Desktop::CarouselFaceout", "auiDeviceType": "desktop", "imageWidth": 160, "schemaVersion": 2, "productDetailsTemplateClass": "PI::P13N::ViewTemplates::ProductDetails::Desktop::Base", "forceFreshWin": 0, "productDataFlavor": "Faceout", "relatedRequestID": "H21WNBAW5EGZX90ND4PN", "maxLineCount": 6 }, "id_list": ["B01M8QSY16:", "B017XBDBI6:", "B01GL5MYCE:", "B0751DHYXC:", "B01AHWOH54:", "B01M7XYENW:", "B01N7FKKXV:", "B07C1NLKS5:", "B00R25QZDC:", "B01AJB1VFW:", "B079K773M7:", "B07DX3W41P:", "B01GL5606A:", "B07654YLSB:", "B01GFL6MZE:", "B00WLI5E3M:", "B01CTE28DG:", "B01BELELVC:", "B00ZY7H91M:", "B077TPG2WK:", "B01G503MC6:", "B01LYZFC4V:", "B00ID9UQYK:", "B07C3T52LB:", "B07DX39RNS:", "B076551MZP:", "B0761RWKPQ:", "B00T8FD9YM:", "B07653JBYS:", "B07G316H74:", "B01FSEBC9K:", "B014QKBVH0:", "B01BVA2I4S:", "B01CVOZNAE:", "B07D19JDH9:", "B018ACDMJK:", "B00V0H83YW:", "B07C432PK3:", "B07B9P4T4V:", "B076H4WWLK:", "B077G3Y86F:", "B077Z7XLJF:", "B01NCFB2BB:", "B01M4I7FMC:", "B01BEVFJCM:", "B01FSEBC8G:", "B07DXCTKB6:", "B01NBHYAR0:", "B07DGWJ887:", "B00SLP58SU:", "B01N55H5AE:", "B013AZCPLS:", "B076PC3NYV:", "B01BVA2JHE:", "B07FF38J8C:", "B07DHGTS81:", "B00R25QZHS:"], "url": "/gp/p13n-shared/faceout-partial", "id_param_name": "asins" }, "baseAsin": "B01GL56060", "name": "desktop-dp-sims_session-similarities", "set_size": 57 } EDIT: Raw string: {"ajax":{"params":{"asinMetadataKeys":"adId","featureId":"SimilaritiesCarousel","reftagPrefix":"pd_sbs_193","widgetTemplateClass":"PI::Similarities::ViewTemplates::Carousel::Desktop","imageHeight":160,"linkGetParameters":"{\"pf_rd_s\":\"desktop-dp-sims\",\"pf_rd_m\":\"A3JWKAKR8XB7XF\",\"pd_rd_r\":\"e672bcd4-b03e-11e8-8dbb-41abd883f66d\",\"pf_rd_r\":\"X5Z293FJ403CC225M759\",\"pf_rd_t\":\"40701\",\"pd_rd_wg\":\"CrGGS\",\"pf_rd_p\":\"946762da-975a-438a-9e2b-a585cbe769b5\",\"pf_rd_i\":\"desktop-dp-sims\",\"pd_rd_w\":\"ktYgt\"}","faceoutTemplateClass":"PI::P13N::ViewTemplates::Product::Desktop::CarouselFaceout","auiDeviceType":"desktop","imageWidth":160,"schemaVersion":2,"productDetailsTemplateClass":"PI::P13N::ViewTemplates::ProductDetails::Desktop::Base","forceFreshWin":0,"productDataFlavor":"Faceout","relatedRequestID":"X5Z293FJ403CC225M759","maxLineCount":6},"id_list":["B07BHS22V6:","B00ITJNHX6:","B07DDGCLZ1:","B017XYQ4X2:","B01LYA8CLG:","B0747T62HS:","B00LHT0I78:","B071D5LL18:","B071NPLTRS:","B00CFMRFO0:","B01N4X1EL9:","B077R4WZ46:","B00YTZSTVY:","B073V5T8G2:","B00CFMRI7E:","B01ARIYIPM:","B0747X16FY:","B00ZWNPJVA:","B01N4WZ4AL:","B00BU662AU:","B07C2NYVMP:","B01FD7ZOB4:","B017M17VTC:","B00YTZST0K:","B07CVSJG6H:","B00V63GQBC:","B00NYBAJJY:","B01MCZ2ZQC:","B078BSJ8TV:","B077QXWJBR:","B07BL5FWVP:","B00N8SPSSU:","B01LXMVFGI:","B06ZY83D2Z:","B00ZQYY9TI:","B0761HT6JJ:","B06XRWB686:","B075XHDQ85:","B01LYJMK02:","B018JWYKRE:","B0759W61P6:","B078ZKNGRS:","B013BJBZBE:","B01LYMTVY2:","B072VMTVGZ:","B077QXW1Z9:","B07CMB96BX:","B07BNXNMZ5:","B01N3CY4Y3:","B018JX3J7U:","B0747T5MY1:","B07CQPTFDB:","B077QW292J:","B00LHT0GLQ:","B01C4B17XG:","B019WD74F4:"],"url":"/gp/p13n-shared/faceout-partial","id_param_name":"asins"},"baseAsin":"B01LS24R2U","name":"desktop-dp-sims_session-similarities","set_size":56}
just use pythons json library import json j1 = """{ "ajax": { "params": { "asinMetadataKeys": "adId", "featureId": "SimilaritiesCarousel", "reftagPrefix": "pd_sbs_60", "widgetTemplateClass": "PI::Similarities::ViewTemplates::Carousel::Desktop", "imageHeight": 160, "faceoutTemplateClass": "PI::P13N::ViewTemplates::Product::Desktop::CarouselFaceout", "auiDeviceType": "desktop", "imageWidth": 160, "schemaVersion": 2, "productDetailsTemplateClass": "PI::P13N::ViewTemplates::ProductDetails::Desktop::Base", "forceFreshWin": 0, "productDataFlavor": "Faceout", "relatedRequestID": "H21WNBAW5EGZX90ND4PN", "maxLineCount": 6 }, "id_list": ["B01M8QSY16:", "B017XBDBI6:", "B01GL5MYCE:", "B0751DHYXC:", "B01AHWOH54:", "B01M7XYENW:", "B01N7FKKXV:", "B07C1NLKS5:", "B00R25QZDC:", "B01AJB1VFW:", "B079K773M7:", "B07DX3W41P:", "B01GL5606A:", "B07654YLSB:", "B01GFL6MZE:", "B00WLI5E3M:", "B01CTE28DG:", "B01BELELVC:", "B00ZY7H91M:", "B077TPG2WK:", "B01G503MC6:", "B01LYZFC4V:", "B00ID9UQYK:", "B07C3T52LB:", "B07DX39RNS:", "B076551MZP:", "B0761RWKPQ:", "B00T8FD9YM:", "B07653JBYS:", "B07G316H74:", "B01FSEBC9K:", "B014QKBVH0:", "B01BVA2I4S:", "B01CVOZNAE:", "B07D19JDH9:", "B018ACDMJK:", "B00V0H83YW:", "B07C432PK3:", "B07B9P4T4V:", "B076H4WWLK:", "B077G3Y86F:", "B077Z7XLJF:", "B01NCFB2BB:", "B01M4I7FMC:", "B01BEVFJCM:", "B01FSEBC8G:", "B07DXCTKB6:", "B01NBHYAR0:", "B07DGWJ887:", "B00SLP58SU:", "B01N55H5AE:", "B013AZCPLS:", "B076PC3NYV:", "B01BVA2JHE:", "B07FF38J8C:", "B07DHGTS81:", "B00R25QZHS:"], "url": "/gp/p13n-shared/faceout-partial", "id_param_name": "asins" }, "baseAsin": "B01GL56060", "name": "desktop-dp-sims_session-similarities", "set_size": 57 }""" d1 = json.loads(j1) id_list = [elem.replace(":", "") for elem in d1["ajax"]['id_list']] id_list Output: ['B01M8QSY16', 'B017XBDBI6', ... 'B00R25QZHS'] I had to remove the line "linkGetParameters : ... " because it seems to be not json conform.
If you are sure that the attribute "id_list" will always be in one line in a similar single-space format after commas and colon, and the json module is not an option, then you can do the following: list( # make sure the result is a list filter( # filter to… None, # …remove any empty items re.split( # split the line of id_list on… r':(?:,\s)?', # …colon and then optional comma and spaces re.search( # search… r'(?<="id_list": \[)((?:"[^"]+:"(?:,\s*)?)+)', j1) # …for the id_list property and its value .group(0) # take the match .replace('"', '') # and drop all double quotes ))) ['B01M8QSY16', 'B017XBDBI6', 'B01GL5MYCE', 'B0751DHYXC', 'B01AHWOH54', 'B01M7XYENW', 'B01N7FKKXV', 'B07C1NLKS5', 'B00R25QZDC', 'B01AJB1VFW', 'B079K773M7', 'B07DX3W41P', 'B01GL5606A', 'B07654YLSB', 'B01GFL6MZE', 'B00WLI5E3M', 'B01CTE28DG', 'B01BELELVC', 'B00ZY7H91M', 'B077TPG2WK', 'B01G503MC6', 'B01LYZFC4V', 'B00ID9UQYK', 'B07C3T52LB', 'B07DX39RNS', 'B076551MZP', 'B0761RWKPQ', 'B00T8FD9YM', 'B07653JBYS', 'B07G316H74', 'B01FSEBC9K', 'B014QKBVH0', 'B01BVA2I4S', 'B01CVOZNAE', 'B07D19JDH9', 'B018ACDMJK', 'B00V0H83YW', 'B07C432PK3', 'B07B9P4T4V', 'B076H4WWLK', 'B077G3Y86F', 'B077Z7XLJF', 'B01NCFB2BB', 'B01M4I7FMC', 'B01BEVFJCM', 'B01FSEBC8G', 'B07DXCTKB6', 'B01NBHYAR0', 'B07DGWJ887', 'B00SLP58SU', 'B01N55H5AE', 'B013AZCPLS', 'B076PC3NYV', 'B01BVA2JHE', 'B07FF38J8C', 'B07DHGTS81', 'B00R25QZHS'] This is dense and mostly unreadable code; use as-is, or I can break down more readably the logic if you want.
Seeing as you can't use the JSON library, you can try this here expression (tested on Python3): result = [ id.strip('":') for id in re.search('"id_list": \[(.*)\],', jsonstr).group(1).split(", ") ] (where jsonstr is a string containing all of the original JSON code). To make it easier to understand, the above code uses re.search (not re.filterall as you had suggested) to broadly locate and select the line, group to narrow down the selection, split to transform the string into a list, and strip to trim off the unnecessary characters in each list item leaving you with a list of IDs like the one you specify in your question.
First, as Florian H stated. You should claim valid JSON from your source in order to be able to use the json Python module. Someone who provides JSON should provide valid JSON... EDIT: The JSON seems valid, see below Trying to use the json module anyway to address your need, I noted that the parsing problem comes from the escaped double-quote in linkGetParameters value. I assume the JSON string has been copied/pasted as is and this is probably the source of the JSON parsing problem. Simply pasting this JSON in a Python string makes Python use the anti-slash to escape the double quote instead of preserving the two characters. To test the JSON content, you have to copy it into a raw string (= prefixed by a r): import json json_ = r"""{ "ajax": { "params": { "asinMetadataKeys": "adId", "featureId": "SimilaritiesCarousel", "reftagPrefix": "pd_sbs_60", "widgetTemplateClass": "PI::Similarities::ViewTemplates::Carousel::Desktop", "imageHeight": 160, "linkGetParameters": "{\"pf_rd_s\":\"desktop-dp-sims\",\"pf_rd_m\":\"A3JWKAKR8XB7XF\",\"pd_rd_r\":\"ac83cd73-b019-11e8-99c8-33d23753c678\",\"pf_rd_r\":\"H21WNBAW5EGZX90ND4PN\",\"pf_rd_t\":\"40701\",\"pd_rd_wg\":\"e6DPw\",\"pf_rd_p\":\"946762da-975a-438a-9e2b-a585cbe769b5\",\"pf_rd_i\":\"desktop-dp-sims\",\"pd_rd_w\":\"xg8TH\"}", "faceoutTemplateClass": "PI::P13N::ViewTemplates::Product::Desktop::CarouselFaceout", "auiDeviceType": "desktop", "imageWidth": 160, "schemaVersion": 2, "productDetailsTemplateClass": "PI::P13N::ViewTemplates::ProductDetails::Desktop::Base", "forceFreshWin": 0, "productDataFlavor": "Faceout", "relatedRequestID": "H21WNBAW5EGZX90ND4PN", "maxLineCount": 6 }, "id_list": ["B01M8QSY16:", "B017XBDBI6:", "B01GL5MYCE:", "B0751DHYXC:", "B01AHWOH54:", "B01M7XYENW:", "B01N7FKKXV:", "B07C1NLKS5:", "B00R25QZDC:", "B01AJB1VFW:", "B079K773M7:", "B07DX3W41P:", "B01GL5606A:", "B07654YLSB:", "B01GFL6MZE:", "B00WLI5E3M:", "B01CTE28DG:", "B01BELELVC:", "B00ZY7H91M:", "B077TPG2WK:", "B01G503MC6:", "B01LYZFC4V:", "B00ID9UQYK:", "B07C3T52LB:", "B07DX39RNS:", "B076551MZP:", "B0761RWKPQ:", "B00T8FD9YM:", "B07653JBYS:", "B07G316H74:", "B01FSEBC9K:", "B014QKBVH0:", "B01BVA2I4S:", "B01CVOZNAE:", "B07D19JDH9:", "B018ACDMJK:", "B00V0H83YW:", "B07C432PK3:", "B07B9P4T4V:", "B076H4WWLK:", "B077G3Y86F:", "B077Z7XLJF:", "B01NCFB2BB:", "B01M4I7FMC:", "B01BEVFJCM:", "B01FSEBC8G:", "B07DXCTKB6:", "B01NBHYAR0:", "B07DGWJ887:", "B00SLP58SU:", "B01N55H5AE:", "B013AZCPLS:", "B076PC3NYV:", "B01BVA2JHE:", "B07FF38J8C:", "B07DHGTS81:", "B00R25QZHS:"], "url": "/gp/p13n-shared/faceout-partial", "id_param_name": "asins" }, "baseAsin": "B01GL56060", "name": "desktop-dp-sims_session-similarities", "set_size": 57 }""" result = json.loads(json_) print [id_[:-1] for id_ in result['ajax']['id_list']] # [u'B01M8QSY16', u'B017XBDBI6', u'B01GL5MYCE', u'B0751DHYXC', u'B01AHWOH54', u'B01M7XYENW', u'B01N7FKKXV', u'B07C1NLKS5', u'B00R25QZDC', u'B01AJB1VFW', u'B079K773M7', u'B07DX3W41P', u'B01GL5606A', u'B07654YLSB', u'B01GFL6MZE', u'B00WLI5E3M', u'B01CTE28DG', u'B01BELELVC', u'B00ZY7H91M', u'B077TPG2WK', u'B01G503MC6', u'B01LYZFC4V', u'B00ID9UQYK', u'B07C3T52LB', u'B07DX39RNS', u'B076551MZP', u'B0761RWKPQ', u'B00T8FD9YM', u'B07653JBYS', u'B07G316H74', u'B01FSEBC9K', u'B014QKBVH0', u'B01BVA2I4S', u'B01CVOZNAE', u'B07D19JDH9', u'B018ACDMJK', u'B00V0H83YW', u'B07C432PK3', u'B07B9P4T4V', u'B076H4WWLK', u'B077G3Y86F', u'B077Z7XLJF', u'B01NCFB2BB', u'B01M4I7FMC', u'B01BEVFJCM', u'B01FSEBC8G', u'B07DXCTKB6', u'B01NBHYAR0', u'B07DGWJ887', u'B00SLP58SU', u'B01N55H5AE', u'B013AZCPLS', u'B076PC3NYV', u'B01BVA2JHE', u'B07FF38J8C', u'B07DHGTS81', u'B00R25QZHS'] Once the id_list retrieved, you can remove the last character of each id using the string slicing. When using JSON content from your original source instead of a litteral string, you should not encounter this kind of escaping problem. If it is really not possible, assuming an id is always 10 characters long, this should do the trick: import re json = """{ "ajax": { "params": { "asinMetadataKeys": "adId", "featureId": "SimilaritiesCarousel", "reftagPrefix": "pd_sbs_60", "widgetTemplateClass": "PI::Similarities::ViewTemplates::Carousel::Desktop", "imageHeight": 160, "linkGetParameters": "{\"pf_rd_s\":\"desktop-dp-sims\",\"pf_rd_m\":\"A3JWKAKR8XB7XF\",\"pd_rd_r\":\"ac83cd73-b019-11e8-99c8-33d23753c678\",\"pf_rd_r\":\"H21WNBAW5EGZX90ND4PN\",\"pf_rd_t\":\"40701\",\"pd_rd_wg\":\"e6DPw\",\"pf_rd_p\":\"946762da-975a-438a-9e2b-a585cbe769b5\",\"pf_rd_i\":\"desktop-dp-sims\",\"pd_rd_w\":\"xg8TH\"}", "faceoutTemplateClass": "PI::P13N::ViewTemplates::Product::Desktop::CarouselFaceout", "auiDeviceType": "desktop", "imageWidth": 160, "schemaVersion": 2, "productDetailsTemplateClass": "PI::P13N::ViewTemplates::ProductDetails::Desktop::Base", "forceFreshWin": 0, "productDataFlavor": "Faceout", "relatedRequestID": "H21WNBAW5EGZX90ND4PN", "maxLineCount": 6 }, "id_list": ["B01M8QSY16:", "B017XBDBI6:", "B01GL5MYCE:", "B0751DHYXC:", "B01AHWOH54:", "B01M7XYENW:", "B01N7FKKXV:", "B07C1NLKS5:", "B00R25QZDC:", "B01AJB1VFW:", "B079K773M7:", "B07DX3W41P:", "B01GL5606A:", "B07654YLSB:", "B01GFL6MZE:", "B00WLI5E3M:", "B01CTE28DG:", "B01BELELVC:", "B00ZY7H91M:", "B077TPG2WK:", "B01G503MC6:", "B01LYZFC4V:", "B00ID9UQYK:", "B07C3T52LB:", "B07DX39RNS:", "B076551MZP:", "B0761RWKPQ:", "B00T8FD9YM:", "B07653JBYS:", "B07G316H74:", "B01FSEBC9K:", "B014QKBVH0:", "B01BVA2I4S:", "B01CVOZNAE:", "B07D19JDH9:", "B018ACDMJK:", "B00V0H83YW:", "B07C432PK3:", "B07B9P4T4V:", "B076H4WWLK:", "B077G3Y86F:", "B077Z7XLJF:", "B01NCFB2BB:", "B01M4I7FMC:", "B01BEVFJCM:", "B01FSEBC8G:", "B07DXCTKB6:", "B01NBHYAR0:", "B07DGWJ887:", "B00SLP58SU:", "B01N55H5AE:", "B013AZCPLS:", "B076PC3NYV:", "B01BVA2JHE:", "B07FF38J8C:", "B07DHGTS81:", "B00R25QZHS:"], "url": "/gp/p13n-shared/faceout-partial", "id_param_name": "asins" }, "baseAsin": "B01GL56060", "name": "desktop-dp-sims_session-similarities", "set_size": 57 }""" # https://regex101.com/r/qxYe9N/11 id_re = re.compile('"([A-Z0-9]{10}):"') result = id_re.findall(json) print result # ['B01M8QSY16', 'B017XBDBI6', 'B01GL5MYCE', 'B0751DHYXC', 'B01AHWOH54', 'B01M7XYENW', 'B01N7FKKXV', 'B07C1NLKS5', 'B00R25QZDC', 'B01AJB1VFW', 'B079K773M7', 'B07DX3W41P', 'B01GL5606A', 'B07654YLSB', 'B01GFL6MZE', 'B00WLI5E3M', 'B01CTE28DG', 'B01BELELVC', 'B00ZY7H91M', 'B077TPG2WK', 'B01G503MC6', 'B01LYZFC4V', 'B00ID9UQYK', 'B07C3T52LB', 'B07DX39RNS', 'B076551MZP', 'B0761RWKPQ', 'B00T8FD9YM', 'B07653JBYS', 'B07G316H74', 'B01FSEBC9K', 'B014QKBVH0', 'B01BVA2I4S', 'B01CVOZNAE', 'B07D19JDH9', 'B018ACDMJK', 'B00V0H83YW', 'B07C432PK3', 'B07B9P4T4V', 'B076H4WWLK', 'B077G3Y86F', 'B077Z7XLJF', 'B01NCFB2BB', 'B01M4I7FMC', 'B01BEVFJCM', 'B01FSEBC8G', 'B07DXCTKB6', 'B01NBHYAR0', 'B07DGWJ887', 'B00SLP58SU', 'B01N55H5AE', 'B013AZCPLS', 'B076PC3NYV', 'B01BVA2JHE', 'B07FF38J8C', 'B07DHGTS81', 'B00R25QZHS']
Dynamically double-quote "keys" in text to form valid JSON string in python
I'm working with text contained in JS variables on a webpage and extracting strings using regex, then turning it into JSON objects in python using json.loads(). The issue I'm having is the unquoted "keys". Right now, I'm doing a series of replacements (code below) to "" each key in each string, but what I want is to dynamically identify any unquoted keys before passing the string into json.loads(). Example 1 with no space after : character json_data1 = '[{storeName:"testName",address:"12345 Road",address2:"Suite 500",city:"testCity",storeImage:"http://www.testLink.com",state:"testState",phone:"999-999-9999",lat:99.9999,lng:-99.9999}]' Example 2 with space after : character json_data2 = '[{storeName: "testName",address: "12345 Road",address2: "Suite 500",city: "testCity",storeImage: "http://www.testLink.com",state: "testState",phone: "999-999-9999",lat: 99.9999,lng: -99.9999}]' Example 3 with space after ,: characters json_data3 = '[{storeName: "testName", address: "12345 Road", address2: "Suite 500", city: "testCity", storeImage: "http://www.testLink.com", state: "testState", phone: "999-999-9999", lat: 99.9999, lng: -99.9999}]' Example 4 with space after : character and newlines json_data4 = '''[ { storeName: "testName", address: "12345 Road", address2: "Suite 500", city: "testCity", storeImage: "http://www.testLink.com", state: "testState", phone: "999-999-9999", lat: 99.9999, lng: -99.9999 }]''' I need to create pattern that identifies which are keys and not random string values containing characters such as the string link in storeImage. In other words, I want to dynamically find keys and double-quote them to use json.loads() and return a valid JSON object. I'm currently replacing each key in the text this way content = re.sub('storeName:', '"storeName":', content) content = re.sub('address:', '"address":', content) content = re.sub('address2:', '"address2":', content) content = re.sub('city:', '"city":', content) content = re.sub('storeImage:', '"storeImage":', content) content = re.sub('state:', '"state":', content) content = re.sub('phone:', '"phone":', content) content = re.sub('lat:', '"lat":', content) content = re.sub('lng:', '"lng":', content) Returned as string representing valid JSON json_data = [{"storeName": "testName", "address": "12345 Road", "address2": "Suite 500", "city": "testCity", "storeImage": "http://www.testLink.com", "state": "testState", "phone": "999-999-9999", "lat": 99.9999, "lng": -99.9999}] I'm sure there is a better way of doing this but I haven't been able to find or come up with a regex pattern to handle these. Any help is greatly appreciated!
Something like this should do the job: ([{,]\s*)([^"':]+)(\s*:) Replace for: \1"\2"\3 Example: https://regex101.com/r/oV0udR/1
That repetition is of course unnecessary. You could put everything into a single regex: content = re.sub(r"\b(storeName|address2?|city|storeImage|state|phone|lat|lng):", r'"\1":', content) \1 contains the match within the first (in this case, only) set of parentheses, so "\1": surrounds it with quotes and adds back the colon. Note the use of a word boundary anchor to make sure we match only those exact words.
Regex: (\w+)\s?:\s?("?[^",]+"?,?) Regex demo import re text = 'storeName: "testName", ' text = re.sub('(\w+)\s?:\s?("?[^",]+"?,?)', "\"\g<1>\":\g<2>", text) print(text) Output: "storeName":"testName",
create a dictionary from file python
I am new to python and am trying to read a file and create a dictionary from it. The format is as follows: .1.3.6.1.4.1.14823.1.1.27 { TYPE = Switch VENDOR = Aruba MODEL = ArubaS3500-48T CERTIFICATION = CERTIFIED CONT = Aruba-Switch HEALTH = ARUBA-Controller VLAN = Dot1q INSTRUMENTATION: Card-Fault = ArubaController:DeviceID CPU/Memory = ArubaController:DeviceID Environment = ArubaSysExt:DeviceID Interface-Fault = MIB2 Interface-Performance = MIB2 Port-Fault = MIB2 Port-Performance = MIB2 } The first line OID (.1.3.6.1.4.1.14823.1.1.27 { ) I want this to be the key and the remaining lines are the values until the } I have tried a few combinations but am not able to get the correct regex to match these Any help please? I have tried something like lines = cache.readlines() for line in lines: searchObj = re.search(r'(^.\d.*{)(.*)$', line) if searchObj: (oid, cert ) = searchObj.groups() results[searchObj(oid)] = ", ".join(line[1:]) print("searchObj.group() : ", searchObj.group(1)) print("searchObj.group(1) : ", searchObj.group(2))
You can try this: import re data = open('filename.txt').read() the_key = re.findall("^\n*[\.\d]+", data) values = [re.split("\s+\=\s+", i) for i in re.findall("[a-zA-Z0-9]+\s*\=\s*[a-zA-Z0-9]+", data)] final_data = {the_key[0]:dict(values)} Output: {'\n.1.3.6.1.4.1.14823.1.1.27': {'VENDOR': 'Aruba', 'CERTIFICATION': 'CERTIFIED', 'Fault': 'MIB2', 'VLAN': 'Dot1q', 'Environment': 'ArubaSysExt', 'HEALTH': 'ARUBA', 'Memory': 'ArubaController', 'Performance': 'MIB2', 'CONT': 'Aruba', 'MODEL': 'ArubaS3500', 'TYPE': 'Switch'}}
You could use a nested dict comprehension along with an outer and inner regex. Your blocks can be separated by .numbers...numbers.. { // values here } In terms of regular expression this can be formulated as ^\s* # start of line + whitespaces, eventually (?P<key>\.[\d.]+)\s* # the key {(?P<values>[^{}]+)} # everything between { and } As you see, we split the parts into key/value pairs. Your "inner" structure can be formulated like (?P<key>\b[A-Z][-/\w]+\b) # the "inner" key \s*=\s* # whitespaces, =, whitespaces (?P<value>.+) # the value Now let's build the "outer" and "inner" expressions together: rx_outer = re.compile(r'^\s*(?P<key>\.[\d.]+)\s*{(?P<values>[^{}]+)}', re.MULTILINE) rx_inner = re.compile(r'(?P<key>\b[A-Z][-/\w]+\b)\s*=\s*(?P<value>.+)') result = {item.group('key'): {match.group('key'): match.group('value') for match in rx_inner.finditer(item.group('values'))} for item in rx_outer.finditer(string)} print(result) A demo can be found on ideone.com.