reading key values from string - python

I'm getting the following output from my POST API request (yaml) and need to get key,value from it. tried to convert to json but didn't work as expected.
x = {'map_key': '- TYPE: UK\nNAME: TOM\n- TYPE: US\nNAME: BOB'}
test = json.dumps(x['map_key'].replace("-",""), indent=4)
How can i get TYPE,NAME values from the above x variable

Looks like you're trying to build a dictionary out of the map_key value. Perhaps something like this:
x = {'map_key': '- TYPE: UK\nNAME: TOM\n- TYPE: US\nNAME: BOB'}
result = {}
for token in x['map_key'].split('\n'):
k, v = token.split(':')
result.setdefault(k.split()[-1], []).append(v.strip())
print(result)
Output:
{'TYPE': ['UK', 'US'], 'NAME': ['TOM', 'BOB']}

Related

How to convert long string into a dictionary by splitting on line breaks and colons

I have the following string that I have created in python:
metadata = """Passengerid: string
Name: string
sex: categorical
Fare: numeric
Fareclass: numeric"""
I would like to create a for loop that unpacks this string and creates a dictionary.
My desired output would be:
dct =
{'Name': 'string',
'sex': 'categorical',
'Fare': 'numeric',
'Fareclass': 'numeric'
}
My instinct to get started is something like:
metadata.split('\n')
Which would yield the following list but is not creating anything that could be obviously turned into a dictionary:
['Passengerid: string',
'Name: string',
'sex: categorical',
'Fare: numeric',
'Fareclass: numeric']
Try this:
metadata = """Passengerid: string
Name: string
sex: categorical
Fare: numeric
Fareclass: numeric"""
metadata = [tuple(m.replace(' ', '').split(':')) for m in metadata.split('\n')]
metadata_dict = {k:v for (k, v) in metadata}
[Update] Alternative approach by Rabinzel:
metadata = """Passengerid: string
Name: string
sex: categorical
Fare: numeric
Fareclass: numeric"""
metadata_dict = dict(tuple(map(str.strip, string.split(':'))) for string in metadata.split('\n'))
This oneliner should work for you:
{x.split(':')[0].strip():x.split(':')[1].strip() for x in metadata.split('\n')}
Output:
{'Passengerid': 'string',
'Name': 'string',
'sex': 'categorical',
'Fare': 'numeric',
'Fareclass': 'numeric'}
using the input data from metadata (triple quoted string) we can do:
my_dict = {}
for line in metadata.splitlines():
k, v = line.split(":")
my_dict.update({k: v})

How to retrieve key value pair within listed dictionary in python?

I have a list of dictionaries as a key value pairs, where I want to access the data of each dict by key:
sample data:
['"imageUrl":"/images/4.jpg"', '"number":"04047122"', '"name":"test"',...
real data
>>> data
['"imageUrl":"/images/products/klein/04047122_k.jpg"', '"art":"04047122"', '"productId":"170336"'; } } }) ']
This unfortunatelly does not work:
re.findall(r'(?:number\(\{)(.*)', data)[0].split(',')
How can I retrieve the values by name e.g. data['number'] ?
For a more robust solution, since each string in the input list is a valid line of CSV, delimited by a colon, you can use csv.reader to parse the list and then pass the resulting sequence of key-value pairs to the dict constructor to build a dict:
import csv
lst = ['"imageUrl":"/images/4.jpg"', '"number":"04047122"', '"name":"test"']
data = dict(csv.reader(lst, delimiter=':'))
You can then access data['number'] as desired.
Try to convert your data to a real dictionary:
data = ['"imageUrl":"/images/4.jpg"', '"number":"04047122"', '"name":"test"']
data_dict = dict([x.replace('"','').split(":") for x in data])
and then you will be able to access your keys:
print(data_dict["number"]) # output: 04047122
You can convert your string list to an actual dictionary easily:
>>> ls = ['"imageUrl":"/images/4.jpg"', '"number":"04047122"', '"name":"test"']
>>> data = dict(elem.replace('"', '').split(':') for elem in ls)
>>> data
{'imageUrl': '/images/4.jpg', 'number': '04047122', 'name': 'test'}
>>> data['number']
'04047122'

Python list formatting with JSON

I'm a newbie in Python trying to turn information from an Excel file into JSON output.
I'm trying to parse this Python list:
value = ['Position: Backstab, Gouge,', 'SumPosition: DoubleParse, Pineapple']
into this JSON format:
"value": [
{
"Position": [
"Backstab, Gouge,"
]
},
{
"SumPosition": [
"DoubleParse, Pineapple"
]
}
]
Please note:
This list was previously a string:
value = 'Position: Backstab, Gouge, SumPosition: DoubleParse, Pineapple'
Which I turned into a list by using re.split().
I've already turned the string into a list by using re.split, but I still can't turn the inside of the string into a dict, and the value from the dict into a list.
Is that even possible? Is it the case to format the list/string with JSON or previously prepare the string itself so it can receive the json.dump method?
Thanks in advance!
You can iterate over the list to achieve desired result.
d = {'value': []}
for val in value:
k, v = val.split(':')
tmp = {k.strip() : [v.strip()]}
d['value'].append(tmp)
print(d)
{'value': [{'Position': ['Backstab, Gouge,']},
{'SumPosition': ['DoubleParse, Pineapple']}]}
Here is a quick way.
value = ['Position: Backstab, Gouge,',
'SumPosition: DoubleParse, Pineapple']
dictionary_result = {}
for line in value:
key, vals = line.split(':')
vals = vals.split(',')
dictionary_result[key] = vals
Remaining tasks for you: trim off empty strings from result lists like [' Backstab', ' Gouge', ''], and actually convert the data from a Python dict to a JSON file

python-Get a string after specific word in line

I'm searching JIRA tickets which have specific subject.I put results in JSON file (whole file:https://1drv.ms/f/s!AizscpxS0QM4attoSBbMLkmKp1s)
I wrote a python code to get ticket description
#!/usr/bin/python
import sys
import json
if sys.version[0] == '2':
reload(sys)
sys.setdefaultencoding("utf-8")
sys.stdout = open('output.txt','wt')
datapath = sys.argv[1]
data = json.load(open(datapath))
for issue in data['issues']:
if len(issue['fields']['subtasks']) == 0 or 'description' in issue['fields']:
custom_field = issue['fields']['description']
my_string=custom_field
#print custom_field
print my_string.split("name:",1)[1]
Some tickets have this value in description:
"description": "name:some name\r\n\r\ncount:5\r\n\r\nregion:some region\r\n\r\n\u00a0",
i need to get values after Name, count and region for all tickets:
desired output (in this example JSON file):
some name 5 some region
some name 5 some region
With code above i can get all values after name
some name^M
^M
count:5^M
^M
region:some region
Also, how to skip processing tickets which have no these values in description, in that case i get:
print custom_field.split("name",1)[2]
IndexError: list index out of range
This looks like a job for a regular expression:
>>> import re
>>> x = r"(\w+):(.+)\r\n\r"
>>> regexp = re.compile(x)
>>> s = "name:some name\r\n\r\ncount:5\r\n\r\nregion:some region\r\n\r\n\u00a0"
>>> regexp.findall(s)
[('name', 'some name'), ('count', '5'), ('region', 'some region')]
Or, if you want a dictionary back,
>>> dict(regexp.findall(s))
{'count': '5', 'region': 'some region', 'name': 'some name'}
You can drop the keys from the dict like this:
>>> mydict = dict(regexp.findall(s))
>>> mydict.values()
mydict.values()
['5', 'some region', 'some name']
But be careful, because they may not be in the order you expect. To match your desired output:
>>> mydict = dict(regexp.findall(s))
>>> print("{name} {count:2s} {region}".format(**mydict))
some name 5 some region
If you don't have the expected values, the findall() call will return an empty or incomplete list. In that case you must check the returned dict before printing it, otherwise the format() call will fail.
One way to ensure that the dict always has the expected values is to set it up beforehand with defaults.
>>> mydict = {'count': 'n/a', 'region': 'n/a', 'name': 'n/a'}
>>> mydict.update(dict(regexp.findall(s)))
Then the format() call will always work, even if one of the fields is missing from the data.
you can use this try catch expression
try:
print custom_field.split("name",1)[2]
except :
print("Skipping ..")

Printing json data in string format

I have many json fields in my model. I want to print them in the string format.
The code I am using is :
data=[]
detail=details.objects.filter(Id=item['Id'])
for i in compliance:
data.append(str("Name")+str(":")+str(i.Name)+str(" , ")+str("Details")+str(":")+str(i.Details)
print data
The output I am getting is :
Name:ABC, Details:{u'Status': u'True', u'Remarks': u'No Remark'}
The expected output is:
Name:ABC, Details:Status:True,Remarks:No Remark
Any help will be appreciated.
Check if your data is of type dict
If not print as you are doing now
If yes then send dictionary to another function which does as below
def print_dict(d):
return ",".join([key+":"+str(d[key]) for key in d])
You can do it this way, assuming compliance is a dict / json.
Save the key dicts in a list
Iterate over that list and build a concatenated list
Code would look like this:
keyorder = ['Name', 'Status', 'Remarks']
res = []
for key in keyorder:
res.append(key + ':' + compliance[key])
', '.join(res)
'Name:ABC, Status:True, Remarks:No remarks'
As #chkri suggested check first if your data is dict if yes then you can try this one line solution:
dict={'Name':'ABC', 'Details':{u'Status': u'True', u'Remarks': u'No Remark'}}
print({k:v for k,v in dict.items()})
output:
{'Name': 'ABC', 'Details': {'Remarks': 'No Remark', 'Status': 'True'}}

Categories

Resources