I'm trying to turn this object into something I can work with in my program. I need to store the message-id to a database. Short of flat-out parsing it, can I just turn this whole thing into a dictionary somehow?
Here is where I'm at with some IDLE testing:
>>> response = urllib.request.urlopen(req)
>>> response.getheaders()
[('Server', 'nginx/1.6.0'), ('Date', 'Sat, 07 Jun 2014 00:32:45 GMT'), ('Content-Type', 'application/json;charset=ISO-8859-1'), ('Transfer-Encoding', 'chunked'), ('Connection', 'close'), ('Cache-Control', 'max-age=1')]
>>> response.read()
b'{"message-count":"1","messages":[{"to":"11234567890","message-id":"02000000300D8F21","status":"0","remaining-balance":"1.82720000","message-price":"0.00620000","network":"302220"}]}'
After a half hour of sifting through google I was able to turn it into a string with:
>>> response.response.decode('utf-8')
>>> print(response)
'{"message-count":"1","messages":[{"to":"11234567890","message-id":"02000000300D8F21","status":"0","remaining-balance":"1.82720000","message-price":"0.00620000","network":"302220"}]}'
>>> type(response)
<class 'str'>
I found this post but here is what I get:
>>> a_dict = dict([response.strip('{}').split(":"),])
Traceback (most recent call last):
File "<pyshell#79>", line 1, in <module>
a_dict = dict([response.strip('{}').split(":"),])
ValueError: dictionary update sequence element #0 has length 9; 2 is required
Maybe I'm going about this all wrong. What's the quickest way to turn this object into a dictionary or something else I can easily work with?
Load it via json.load():
import json
response = urllib.request.urlopen(req)
result = json.loads(response.readall().decode('utf-8'))
message_id = result['messages'][0]['message-id']
Related
I have a strange problem I don't get. I have a format string with a lot of fields. I want to supply the content for the fields using a list. The following simple demo below shows the issue:
>>> formatstr = "Hello {}, you are my {} fried since {}"
>>> list = ["John", "best", 2020]
>>> print formatstr.format(list)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>>
The format string has 3 fields and the list has also 3 elements.
So I don't understand the error message.
Even when I try to address the indexes within the format string:
>>>
>>> formatstr = "Hello {0:}, you are my {1:} fried since {2:}"
>>>
>>> print formatstr.format(list)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>>
Can you please help me? I think I blocked somewhere in my thinking.
Thanks.
I am trying to load a string (the actual program read this line from a file and it is a very large file that I can not manually modify) formatted as a dictionary.
I need to convert the string line into a json object so I can check value of specific key, e.g. myJson[Date] .
This is the script:
import json
mystring = "{'Date': 'Fri, 19 Apr 2019 03:58:04 GMT', 'Server': 'Apache/2.4.39', 'Accept-Ranges': 'bytes'}"
mystring = json.dumps(mystring)
myJson = json.loads(mystring)
print(str(myJson.keys()))
print(str(myJson))
I am getting this error:
AttributeError: 'str' object has no attribute 'keys'
I suspect that the mystring format is not conforming and that the single quotes should be double quotes? Given that I have a large data, and I can not simply replace single colons with double one using simple search/replace as single colons may be included in the values which I should not modify. If this is the cause of the problem, is there any way to replace the colons of the key/value pair only without touching the colons in the values? I am hoping that this is not the problem.
Rather than dealing with the single quoted string and struggling to convert it into json, just use ast package to convert it into a valid dict.
import ast
mystring = "{'Date': 'Fri, 19 Apr 2019 03:58:04 GMT', 'Server': 'Apache/2.4.39', 'Accept-Ranges': 'bytes'}"
my_dict = ast.literal_eval(mystring)
the result is:
> print(my_dict["Date"])
Fri, 19 Apr 2019 03:58:04 GMT
This code stores the string as a dictionary in a variable called "Tempvar"
From that variable you can just use the keys like a regular dictionary.
import json
mystring = "{'Date': 'Fri, 19 Apr 2019 03:58:04 GMT', 'Server': 'Apache/2.4.39', 'Accept-Ranges': 'bytes'}"
exec("tempvar = " + mystring)
mystring = json.dumps(mystring)
myJson = json.loads(mystring)
print(str(tempvar['Date']))
print(str(myJson))
Hope this helps
Yes json decoder likes double quotes for keys and values, and I think you can use python to do the replacement, try if applies:
mystring = "{'Date': 'Fri, 19 Apr 2019 03:58:04 GMT', 'Server': 'Apache/2.4.39', 'Accept-Ranges': 'bytes'}"
json_string = mystring.replace("'", "\"")
d = json.loads(json_string)
dstring = json.dumps(d)
myJson = json.loads(dstring)
print(str(myJson.keys()))
Looks like I got a bug when I try:
>>> from Bio.Align import AlignInfo
>>> summary_align = AlignInfo.SummaryInfo('/home/ivan/Elasmo only')
>>> consensus = summary_align.dumb_consensus()
Traceback (most recent call last):
File "<pyshell#148>", line 1, in <module>
consensus = summary_align.dumb_consensus()
File "/usr/local/lib/python2.7/dist-packages/Bio/Align/AlignInfo.py", line 76, in dumb_consensus
con_len = self.alignment.get_alignment_length()
AttributeError: 'str' object has no attribute 'get_alignment_length'
Hope somebody can help me.
Cheers,
You instantiated the SummaryInfo class with a string not an Alignment
object.
You are trying to call .dumb_consensus() on a string, but this method will only work if you instantiate the SummaryInfo class with an Alignment, not a string.
http://biopython.org/DIST/docs/api/Bio.Align.Generic.Alignment-class.html#get_alignment_length
try this:
# Make an example alignment object
>>> from Bio.Align.Generic import Alignment
>>> from Bio.Alphabet import IUPAC, Gapped
>>> align = Alignment(Gapped(IUPAC.unambiguous_dna, "-"))
>>> align.add_sequence("Alpha", "ACTGCTAGCTAG")
>>> align.add_sequence("Beta", "ACT-CTAGCTAG")
>>> align.add_sequence("Gamma", "ACTGCTAGATAG")
# Instantiate SummaryInfo class and pass 'align' to it.
>>> from Bio.Align import AlignInfo
>>> summary_align = AlignInfo.SummaryInfo(align)
>>> consensus = summary_align.dumb_consensus()
Just as a note, it looks like the Alignment object is becoming depreciated so you may look into using MultipleSeqAlignment.
It gives me an error that the line encoded needs to be bytes not str/dict
I know of adding a "b" before the text will solve that and print the encoded thing.
import base64
s = base64.b64encode(b'12345')
print(s)
>>b'MTIzNDU='
But how do I encode a variable?
such as
import base64
s = "12345"
s2 = base64.b64encode(s)
print(s2)
It gives me an error with the b added and without. I don't understand
I'm also trying to encode/decode a dictionary with base64.
You need to encode the unicode string. If it's just normal characters, you can use ASCII. If it might have other characters in it, or just for general safety, you probably want utf-8.
>>> import base64
>>> s = "12345"
>>> s2 = base64.b64encode(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ". . . /lib/python3.3/base64.py", line 58, in b64encode
raise TypeError("expected bytes, not %s" % s.__class__.__name__)
TypeError: expected bytes, not str
>>> s2 = base64.b64encode(s.encode('ascii'))
>>> print(s2)
b'MTIzNDU='
>>>
I'm trying to send urlencode() data to my web server.
The data used for the urlencode() function is read from a text file located on my local machine.
When I read the input data for the urlencode() function from the .py script no error is being thrown.
However, if the input data for the urlencode() function is comming from a local input text file I get the following error:
Traceback (most recent call last):
File "active_directory_ssl_test.py", line 30, in
params = urllib.urlencode(dict(LINE))
ValueError: dictionary update sequence element #0 has length 1; 2 is required
I'm doing the following:
FILE=open(IN_FILE, 'r')
LINE = FILE.readline()
while LINE:
print LINE
LINE = FILE.readline()
params = urllib.urlencode(dict(LINE))
try:
f_handler = urlopen('https://host_name/path_name/file_name', params)
Why is there a difference, an error, when reading data from a text file. In both cases a variable is used as a parameter to the urlencode() function.
This is the content of the input text file:
{'hostname' : 'host.1.com', 'port' : '389', 'basedn' : 'CN=Users,DC=prem,DC=local', 'username' : 'CN=Administrator,CN=Users,DC=onprem,DC=local', 'password' : 'passwd', 'roupname' : 'CN=Group,CN=Users,DC=onprem,DC=local', 'attribute' : 'name', 'enabled' : 'sync', 'impsync' : 'sync', 'enabled' : 'enabled', 'username' : 'user#1.com', 'password' : 'passwd', 'update' ; 'update'}
I'll go ahead and post my comment as an answer, as it is the answer. You're calling dict() on a string. The dict() function expects one of two types of input. Either A. a list of tuples that form (key, value) pairs, or B. keyword arguments that come in the form key = value. You're not passing either of those.
-- Extra detail for the Comments --
>>> input = {'key1': 'value1', 'key2': 'value2'}
>>> type(input)
<type 'dict'>
>>> dict(input)
{'key2': 'value2', 'key1': 'value1'}
>>> input = "{'key1': 'value1', 'key2': 'value2'}" # This is your 2nd form.
>>> type(input)
<type 'str'>
>>> dict(input)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: dictionary update sequence element #0 has length 1; 2 is required
Also, for what it's worth, in your first example the call to dict() is superfluous. You already have a dictionary which was declared using literal syntax.