Convert String to json python [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have this type of string given by my lambda function
aws-internal/3 aws-sdk-java/1.11.432
Linux/4.9.124-0.1.ac.198.71.329.metal1.x86_64
OpenJDK_64-Bit_Server_VM/25.181-b13 java/1.8.0_181
Now i want to convert this string into Json like
{
aws-internal:3,
aws-sdk-java:1.11.432,
Linux:/4.9.124-0.1.ac.198.71.329.metal1.x86_64
}
Please Give me a suggestion i search many website and library

You can do that with something like:
items = dict(a.split('/') for a in a_string.split())
Test Code:
a_string='aws-internal/3 aws-sdk-java/1.11.432 Linux/4.9.124-0.1.ac.198.71.329.metal1.x86_64 OpenJDK_64-Bit_Server_VM/25.181-b13 java/1.8.0_181'
items = dict(a.split('/') for a in a_string.split())
print(items)
Results
{
'aws-internal': '3',
'aws-sdk-java': '1.11.432',
'Linux': '4.9.124-0.1.ac.198.71.329.metal1.x86_64',
'OpenJDK_64-Bit_Server_VM': '25.181-b13',
'java': '1.8.0_181'
}

Related

python handle json list format to array list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
use python 3.6
this json: [["10000000000"], ["12000000000"]]
i want get like result like
"data": {"calleeInfo": [{"phone": "10000000000",},{"phone": "12000000000",}]}
By list generation
temp = [["10000000000"], ["12000000000"]]
value = [{"phone": i[0]} for i in temp]
result = {"data": {"calleeInfo": value}}
print(result)
Output
{'data': {'calleeInfo': [{'phone': '10000000000'}, {'phone': '12000000000'}]}}

Grab specific text from string in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How can I grab the invite code from this string?
{awarded:1,inviteURL:https:\/\/www.example.com\/refer\/invite\/111A111A\/}
The expected output would be "111A111A".
Any help is appreciated
I tried it in a simple way, You could give more details for further improvement.
s = "{awarded:1,inviteURL:https:\/\/www.example.com\/refer\/invite\/111A111A\/}"
print(s[-11: -3])
This will do it with ReGex
import re
def findInvite(s):
return re.search(r"(?<=/invite\\/).*(?=\\/)",s).group()
assert findInvite("{awarded:1,inviteURL:https:\/\/www.example.com\/refer\/invite\/111A111A\/}") == "111A111A"
And if this isn't a string but a dict, then change the function to:
def findInvite(d):
s = d["inviteURL"]
return re.search(r"(?<=/invite\\/).*(?=\\/)",s).group()

Regex to match up to the last occurrence of "$" and replace everything to the right [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Context: I want to use Python to iterate over a bunch of json documents (cosmodb) and remove the last portion of a string in the value of a key
I want to turn this:
"id": "randomstuff-channelruleprintermap-1234-$pc2$randomstuff$1234$uspc02"
into this:
"id": "randomstuff-channelruleprintermap-1234-$pc2$randomstuff$"
Any help would be greatly appreciated.
You could easily implement this with this piece of code:
result = my_string.rsplit('$', 1)[0] + "$"
Which behaves like this:
>>> my_string = "randomstuff-channelruleprintermap-1234-$pc2$randomstuff$1234$uspc02"
>>> print(my_string.rsplit('_', 1)[0])
"randomstuff-channelruleprintermap-1234-$pc2$randomstuff$"

string concatination at specificv value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
these items are in a list
SF-04-08-010-MD01,
AHU-VVIP-02-003-MD03,
AHU-02-17-019-DPS03,
AHU-T3-01-PL-TS01,
EF-03-32-108-MD01,
AHU-02-16-019-MD01,
AHU-T3-01-003-MD01,
SF-04-08-010-MD01,
AHU-VVIP-02-003-MD03,
so i want a new list which should be like
SF-04-08
AHU-VVIP
AHU-02-17
AHU-T3-01
EF-03-32
AHU-02-16
AHU-T3-01
SF-04-08
AHU-VVIP-02
using python??
you can use strip like:
data = ['SF-04-08-010-MD01',
'AHU-VVIP-02-003-MD03',
'AHU-02-17-019-DPS03'
]
for item in data:
print ('-'.join(item.split('-')[:3]))
output:
SF-04-08
AHU-VVIP-02
AHU-02-17

how to get projectID value from dictionary in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
need to extract projectID and my final output is to be 1868117666669.
'{
"code":"ok",
"job":{
"config":{
"progress":{
"message":"Reading 2014-03-03__12-57-01-PM.xml",
"percent":107,
"maxmemory":954,
"memory":667
},
"projectID":1868117666669,
# other stuff
}
}
}'
You can try
>>> a = '{"code":"ok","job":{"config":{"progress":{"message":"Reading 2014-03-03__12-57-01-PM.xml","percent":107, "maxmemory":954,"memory":667},"projectID":1868117666669}}}'
>>> json.loads(a)['job']['config']['projectID']
1868117666669

Categories

Resources