This question already has answers here:
How to get a string after a specific substring?
(9 answers)
Closed 5 years ago.
How can I remove letters before certain character? I need to remove every letter from following string until “[“ character and redirect output to .csv file.
{"__metadata": {"uri": loremipsum etc [ {rest of the document}
As per your provided info and required fields, i will suggest that if your JSON data is in a file then you can use:
import json
data = {}
with open("path to json file") as f:
data = json.loads(f.read())
or if your data is stored in string then you can simply do
data = json.loads("json data string")
now you have data in a python dictionary object. now you can easily get any field from the object e.g getting field "cuid" from the first object in entries list:
print data["entries"][0]["cuid"]
or alternatively you can loop on entries list and get all the fields you need e.g
for entry in data["entries"]:
print entry["cuid"]
print entry["name"]
print entry["id"]
print entry["type"]
Find the position of '[' and get the string after that position
print s[s.find("[")+1:].strip()
Sample output:
{rest of the document}
Hope it helps!
You can split from the first occurence and take the rest like :
>>> string = "Egg[spam:miam"
>>> string.split("[", 1)[1]
>>> spam:miam
OR
>>> string = "Egg[spam:miam"
>>> string[string.index("[") + 1:]
>>> spam:miam
Related
i have following string in python
b'{"personId":"65a83de6-b512-4410-81d2-ada57f18112a","persistedFaceIds":["792b31df-403f-4378-911b-8c06c06be8fa"],"name":"waqas"}'
I want to print the all alphabet next to keyword "name" such that my output should be
waqas
Note the waqas can be changed to any number so i want print any name next to keyword name using string operation or regex?
First you need to decode the string since it is binary b. Then use literal eval to make the dictionary, then you can access by key
>>> s = b'{"personId":"65a83de6-b512-4410-81d2-ada57f18112a","persistedFaceIds":["792b31df-403f-4378-911b-8c06c06be8fa"],"name":"waqas"}'
>>> import ast
>>> ast.literal_eval(s.decode())['name']
'waqas'
It is likely you should be reading your data into your program in a different manner than you are doing now.
If I assume your data is inside a JSON file, try something like the following, using the built-in json module:
import json
with open(filename) as fp:
data = json.load(fp)
print(data['name'])
if you want a more algorithmic way to extract the value of name:
s = b'{"personId":"65a83de6-b512-4410-81d2-ada57f18112a",\
"persistedFaceIds":["792b31df-403f-4378-911b-8c06c06be8fa"],\
"name":"waqas"}'
s = s.decode("utf-8")
key = '"name":"'
start = s.find(key) + len(key)
stop = s.find('"', start + 1)
extracted_string = s[start : stop]
print(extracted_string)
output
waqas
You can convert the string into a dictionary with json.loads()
import json
mystring = b'{"personId":"65a83de6-b512-4410-81d2-ada57f18112a","persistedFaceIds":["792b31df-403f-4378-911b-8c06c06be8fa"],"name":"waqas"}'
mydict = json.loads(mystring)
print(mydict["name"])
# output 'waqas'
First you need to convert the string into a proper JSON Format by removing b from the string using substring in python suppose you have a variable x :
import json
x = x[1:];
dict = json.loads(x) //convert JSON string into dictionary
print(dict["name"])
I need to get a count of the emails received daily in a Gmail mailbox and save that count in a table. To do this I am using the imaplib:
import imaplib
import re
obj = imaplib.IMAP4_SSL('imap.gmail.com', 993)
obj.login('xxx#gmail.com', 'password')
obj.select('Inbox')
('OK', ['50'])
gmail_count = obj.search(None,'(SINCE "01-Sep-2020" BEFORE "02-Sep-2020")')
print(gmail_count)
and I get something like this:
('OK', [b'28410 28411 28412 28413 28414 28415 28416 28417 28418 28419 28420 28421 28422 28423 28424 28425 28426 28427 28428 28429 28430 28431 28432 28433 28434 28435 28436 28437 28438 28439'])
I now need to count how many values are in this tuple to get the number of emails so I would like to replace the spaces with commas. How Can I do this?
You can use the replace built-in function to replace spaces by comas in a string
In your case you firstly have to convert your bytes array in a string with the decode("utf-8") function.
If your data are not utf-8 encoded you can change the parameter in the decode function with the correct encoding method.
values = gmail_count[1][0]
replaced = values.decode("utf-8").replace(" ", ",")
You can also just use split to get a list, and then you can get the count and do any other list operations on it:
uidbytes = b’10 20 30’
uidlist = uidbytes.split(b’ ‘)
print(uidlist)
print(len(uidlist))
Output:
[b’10’, b’20, b’30’]
3
You can even change them to integers:
>>> [int(x) for x in uidlist]
[10, 20, 30]
This question already has answers here:
How can I fill out a Python string with spaces?
(14 answers)
Closed 2 years ago.
I am writing a file which gets information about stock items from a csv, the issue is that most of the stock item IDs are 4 digits long, but some of them are 2 or 3 digits, and the remaining digits are replaced with apostrophes ( 872' or 99'' for example). Because the user can pass in specific stock item IDs it would be better if they did not have to include apostrophes in their input just so the code runs, so I want to append apostrophes to their input ID.
At the moment, the stock item IDs to get information for are retrieved using this code::
if args.ID:
if args.ID[0].endswith('.txt'):
with open(args.ID[0], 'r') as f:
IDs = [line for line in f]
else:
IDs = args.FTID
else:
IDs = [ID[25:29] for ID in df['Stock Items'].unique()]
Then I iterate through the dataframe:
for index, row in df.iterrows():
if row['Stock Items'][25:29] in FTIDs:
# Processing
I need to be able to make sure that any input IDs are in the format above.
If you have str and you are 100% sure it is not longer than 4, you can use .ljust to get str with required number of ' added following way
myid = "99"
formatted_id = myid.ljust(4, "'")
print(formatted_id)
Output:
99''
This question already has answers here:
How to convert a file into a dictionary?
(11 answers)
Closed 2 years ago.
If I had a text file with the following texts:
string, float
string 2, float 2
string 3, float 3
... and so on
How would I turn this into a python dictionary?
Ultimately, I would like all of my strings to become the key and all of the floats to become the value.
I have tried turning this into a set, yet I was unable to get to where I wanted it to be.
I have also tried the following code, as I saw another post with a similar problem giving me this solution. Yet, I was unable to get it to print anything.
m={}
for line in file:
x = line.replace(",","") # remove comma if present
y=x.split(':') #split key and value
m[y[0]] = y[1]
Thank you so much.
If every line in the text file is formatted exactly as it is in the example, then this is what I would do:
m = {}
for line in file:
comma = line.find(", ") # returns the index of where the comma is
s = line[:comma]
f = line[comma+1:]
m[s] = str.strip(f) # str.strip() removes the extra spaces
You need to research more. Don't be lazy.
m = {}
for line in file:
(key, value) = line.split(',') # split into two parts
m[key] = value.strip('\n') # remove the line break and append to dictionary
# output
# {'string1': ' 10', 'string2': ' 11'}
I am simply trying to keep the following input and resulting JSON string in order.
Here is the input string and code:
import json
testlist=[]
# we create a list as a tuple so the dictionary order stays correct
testlist=[({"header":{"stream":2,"function":3,"reply":True},"body": [({"format": "A", "value":"This is some text"})]})]
print 'py data string: '
print testlist
data_string = json.dumps(testlist)
print 'json string: '
print data_string
Here is the output string:
json string:
[{"body": [{"format": "A", "value": "This is some text"}], "header": {"stream": 2, "function": 3, "reply": true}}]
I am trying to keep the order of the output the same as the input.
Any help would be great. I can't seem to figure this one point.
As Laurent wrote your question is not very clear, but I give it a try:
OrderedDict.update adds in the above case the entries of databody to the dictionary.
What you seem to want to do is something like data['body'] = databody where databody is this list
[{"format":"A","value":"This is a text\nthat I am sending\n to a file"},{"format":"U6","value":5},{"format":"Boolean","value":true}, "format":"F4", "value":8.10}]
So build first this list end then add it to your dictionary plus what you wrote in your post is that the final variable to be parse into json is a list so do data_string = json.dumps([data])