Related
import json
a_json = '{"some_body":"
[{"someId":"189353945391","EId":"09358039485","someUID":10,"LegalId":"T743","cDate":"202452","rAmount":{"aPa":{"am":1500,"currId":"UD"},"cost":{"amount":1000,"currId":"US"},"lPrice":{"amount":100,"currId":"DD"}},"tes":{"ant":0,"currId":"US"},"toount":{"amnt":0,"currId":"US"},"toount":{"amt":210,"currId":"US"},"bry":"US","pay":[{"pId":"7111","axt":{"amt":2000,"currId":"US"},"mKey":"CSD"}],"oItems":[{"iIndex":0,"rId":"69823","provId":"001","segEntityId":"C001","per":{"vae":1,"ut":"MOS"},"pct":{"prod":"748"},"revType":"REW","rAmount":{"aPaid":{"amt":90000,"currId":"US"},"xt":{"amt":0,"currId":"USD"},"lPrice":{"amt":90000,"currId":"US"}},"stion":{"sLocal":"094u5304","eLocal":"3459340"},"tx":{"adt":{"adet":0,"currId":"US"},"era":"werTIC"}}}]"}'
Above is the JSON I am trying to iterate through. Kind of a weird JSON. I want to take that JSON string and turn it into a python dictionary, so I can better iterate it. I put a single quote mark on either side and used json.loads to turn it into a python dictionary.
loaded_body = json.loads(a_json)
And I am calling the only key from the beginning "some_body" which will get the contents of the entire body. When I run the following command print(loaded_body) I get back the error:
expecting ',' delimiter: line 1 column 18 (char 17)
Ultimately once that error is resolved, this is what I am trying to access:
desired_item =loaded_body['oItems'][0]['rAmount']['aPa']['currId']
Thanks
It seems that you're treating the content of some_body as a string since it's enclosed with double quotes. But inside of that string there's also quotation marks and now it's interpreted that the content of some_body is [{ and then it breaks because directly after that is someId rather than a comma. Thus the error:
expecting ',' delimiter: line 1 column 18 (char 17)
If the content of some_body was actually meant to be a string then all the double quotes inside of it should be preceded by a double backslash (\\) although in this case you'd have to parse the JSON twice - first the entire a_json string and then the content of some_body. However I think it would be easier to just remove the double quotes around the content of some_body.
a_json = '{"some_body":
[{"someId":"189353945391","EId":"09358039485","someUID":10,"LegalId":"T743","cDate":"202452","rAmount":{"aPa":{"am":1500,"currId":"UD"},"cost":{"amount":1000,"currId":"US"},"lPrice":{"amount":100,"currId":"DD"}},"tes":{"ant":0,"currId":"US"},"toount":{"amnt":0,"currId":"US"},"toount":{"amt":210,"currId":"US"},"bry":"US","pay":[{"pId":"7111","axt":{"amt":2000,"currId":"US"},"mKey":"CSD"}],"oItems":[{"iIndex":0,"rId":"69823","provId":"001","segEntityId":"C001","per":{"vae":1,"ut":"MOS"},"pct":{"prod":"748"},"revType":"REW","rAmount":{"aPaid":{"amt":90000,"currId":"US"},"xt":{"amt":0,"currId":"USD"},"lPrice":{"amt":90000,"currId":"US"}},"stion":{"sLocal":"094u5304","eLocal":"3459340"},"tx":{"adt":{"adet":0,"currId":"US"},"era":"werTIC"
}}]}]}'
Also note that there's a missing closing square bracket near the end.
In your original string:
"werTIC"}}}]"}
Which should probably be:
"werTIC"}}]}]}
Now the desired item should look like this (I assume that 'aPa' was meant to be 'aPaid'):
desired_item = loaded_body['some_body'][0]['oItems'][0]['rAmount']['aPaid']['currId']
print(desired_item)
US
I've got a JSON file that was converted to a string in Python. Somehow along the way the double quotes have gotten replaced with single quotes.
{\'MyJSON\': {\'Report\': \'1\' ....
I need to convert my string so that it is in this format instead:
{\"MyJSON\": {\"Report\": \"1\" ....
My problem is that using str.replace, I can't figure out how to convert a single quote into a double quote as both quotes are escaped.
My ultimate goal is to be able to put the string into json.loads so that I can pretty print it.
Attempts:
txt.replace(r"\'", r'\"')
> "{'MyJSON': {'Report': '1'"
txt.replace("\"", "\'")
> "{'MyJSON': {'Report': '1'"
If I save my string to a txt file it appears in the preview as:
{'MyJSON': {'Report': '1' ...
So I think what I actually need to do is replace ' with "
I have decided to use ast.literal_eval(txt) which can convert my string to a dictionary. From there, json.loads(json.dumps(dict)) gets me to JSON
i mean,
my_string = "\"\'"
print(my_string.replace("\'", "\""))
works perfectly fine
EDIT: i didn't mean use this directly, it was a proof of concept. In mine the replacement was reversed. I have updated this snippet such that it could directly be put into your code. Try it again
Instead of focusing on the backslashes to try to "hack" a json string / dict str into a JSON, a better solution is to take it one step at a time and start by converting my dict string into a dictionary.
import ast
txt = ast.literal_eval(txt) # convert my string to a dictionary
txt = json.loads(json.dumps(txt)) # convert my dict to JSON
I am trying to read the following string into a dictionary using Python json package
However, under one of the subfield 'name' there is a description with a nested double quote. My json is unable to read the string that way
import json
string1 =
'{"id":17033,"project_id":17033,"state":"active","state_changed_at":1488054590,"name":"a.k.a.:\xa0"The Sunshine Makers""'
json.loads(string1)
A ERROR was raised
JSONDecodeError: Expecting ',' delimiter: line 1 column 96 (char 95)
I know that the reason for this error was due to the nested double quote around "The Sunshine Makers"
How to I get rid of this double quote?
More examples of string that cause error
string2 = '{"id":960066,"project_id":960066,"state":"active","state_changed_at":1502049940,"name":"New J. Lye Album - Behind The Lyes","blurb":"I am working on my new project titled "Behind The Lyes" which is coming out fall of 2017."'
#The problem with this string comes from the nested double quote around the pharse "Behind The Lyes inside" the 'blurb' subfield
Note that your string has more than one issue making it invalid JSON:
The error you're seeing is the \xa0 (a non-breaking space). That needs to be addressed before the "" issue becomes a problem.
Your string is missing a closing }.
That said, for the string you've cited first, one approach to fixing your issues would be to use .replace():
string1 = '{"id":17033,"project_id":17033,"state":"active","state_changed_at":1488054590,"name":"a.k.a.:\xa0"The Sunshine Makers""'.replace('\xa0"', "'").replace('""', "'\"") + '}'
For example, the following handles the double quoting and other issues in your two sample strings:
import json
fixes = [('\xa0', ' '),('"',"'"),("{'",'{"'),("','", '","'),(",'", ',"'),("':'", '":"'),("':", '":'),("''", '\'\"'), ("'}",'"}')]
print(fixes)
string1 = '{"id":17033,"project_id":17033,"state":"active","state_changed_at":1488054590,"name":"a.k.a.:\xa0"The Sunshine Makers""'
string2 = '{"id":960066,"project_id":960066,"state":"active","state_changed_at":1502049940,"name":"New J. Lye Album - Behind The Lyes","blurb":"I am working on my new project titled "Behind The Lyes" which is coming out fall of 2017."'
strings = [string1, string2]
for string in strings:
print(string)
string = string + '}'
for fix in fixes:
string = string.replace(*fix)
print(string)
print(json.loads(string)['name'])
It would be helpful if you could fill out your question with the code or file from which you are retrieving these strings. That would make it possible to give a more comprehensive answer.
I'm trying to use eval function to execute a loop. It gives a syntax error
list_subjects = (element.upper() for element in list(score_card_data['subject_id']))
for i,sub in enumerate(list_subjects) :
print(("bins_{1:s}").format(i,sub))
print("list(score_card_data.loc[score_card_data['subject_id'] == {1:s}, 'bin_list'])").format(i,sub)
eval("("bins_{1:s}").format(i,sub) = "list(score_card_data.loc[score_card_data['subject_id'] == {1:s}, 'bin_list'])").format(i,sub)")
File "<ipython-input-192-529c79a094e4>", line 5
eval("("bins_{1:s}").format(i,sub) = "list(score_card_data.loc[score_card_data['subject_id'] == {1:s}, 'bin_list'])").format(i,sub)")
^
SyntaxError: invalid syntax
How do I resolve the 2 print statements in one eval function
You get a syntax error because you try to use the same type of quotes inside a string that is used to delimit the string literal in your code.
You have those options:
Use single quotes inside the string and double quotes to delimit it:
eval("' '.join('some', 'words')")
Use double quotes inside the string and single quotes to delimit it:
eval('" ".join("some", "words")')
Use any quotes inside the string and any quotes (the same type on the left and right side of course) to delimit it, but escape all quotation marks inside the string using a backslash:
eval('\' \'.join(\'some\', \'words\')')
eval("\" \".join(\"some\", \"words\")")
Use any quotes inside the string and "triple quotes" (either three single quotes ''' or three double quotes """, same type on the left and right side of course) to delimit it:
eval("""" ".join("some", "words")""")
eval("""' '.join('some', 'words')""")
eval('''' '.join('some', 'words')''')
eval('''" ".join("some", "words")''')
I have the above-mentioned error in s1="some very long string............"
Does anyone know what I am doing wrong?
You are not putting a " before the end of the line.
Use """ if you want to do this:
""" a very long string ......
....that can span multiple lines
"""
I had this problem - I eventually worked out that the reason was that I'd included \ characters in the string. If you have any of these, "escape" them with \\ and it should work fine.
(Assuming you don't have/want line breaks in your string...)
How long is this string really?
I suspect there is a limit to how long a line read from a file or from the commandline can be, and because the end of the line gets choped off the parser sees something like s1="some very long string.......... (without an ending ") and thus throws a parsing error?
You can split long lines up in multiple lines by escaping linebreaks in your source like this:
s1="some very long string.....\
...\
...."
In my situation, I had \r\n in my single-quoted dictionary strings. I replaced all instances of \r with \\r and \n with \\n and it fixed my issue, properly returning escaped line breaks in the eval'ed dict.
ast.literal_eval(my_str.replace('\r','\\r').replace('\n','\\n'))
.....
I faced a similar problem. I had a string which contained path to a folder in Windows e.g. C:\Users\ The problem is that \ is an escape character and so in order to use it in strings you need to add one more \.
Incorrect: C:\Users\
Correct: C:\\Users\\
You can try this:
s = r'long\annoying\path'
I too had this problem, though there were answers here I want to an important point to this
after
/ there should not be empty spaces.Be Aware of it
I also had this exact error message, for me the problem was fixed by adding an " \"
It turns out that my long string, broken into about eight lines with " \" at the very end, was missing a " \" on one line.
Python IDLE didn't specify a line number that this error was on, but it red-highlighted a totally correct variable assignment statement, throwing me off. The actual misshapen string statement (multiple lines long with " \") was adjacent to the statement being highlighted. Maybe this will help someone else.
In my case, I use Windows so I have to use double quotes instead of single.
C:\Users\Dr. Printer>python -mtimeit -s"a = 0"
100000000 loops, best of 3: 0.011 usec per loop
In my case with Mac OS X, I had the following statement:
model.export_srcpkg(platform, toolchain, 'mymodel_pkg.zip', 'mymodel.dylib’)
I was getting the error:
File "<stdin>", line 1
model.export_srcpkg(platform, toolchain, 'mymodel_pkg.zip', 'mymodel.dylib’)
^
SyntaxError: EOL while scanning string literal
After I change to:
model.export_srcpkg(platform, toolchain, "mymodel_pkg.zip", "mymodel.dylib")
It worked...
David
In my case, I forgot (' or ") at the end of string. E.g 'ABC' or "ABC"
I was getting this error in postgresql function. I had a long SQL which I broke into multiple lines with \ for better readability. However, that was the problem. I removed all and made them in one line to fix the issue. I was using pgadmin III.
Your variable(s1) spans multiple lines. In order to do this (i.e you want your string to span multiple lines), you have to use triple quotes(""").
s1="""some very long
string............"""
In this case, three single quotations or three double quotations both will work!
For example:
"""Parameters:
...Type something.....
.....finishing statement"""
OR
'''Parameters:
...Type something.....
.....finishing statement'''
I had faced the same problem while accessing any hard drive directory.
Then I solved it in this way.
import os
os.startfile("D:\folder_name\file_name") #running shortcut
os.startfile("F:") #accessing directory
The picture above shows an error and resolved output.
All code below was tested with Python 3.8.3
Simplest -- just use triple quotes.
Either single:
long_string = '''some
very
long
string
............'''
or double:
long_string = """some
very
long
string
............"""
Note: triple quoted strings retain indentation, it means that
long_string = """some
very
long
string
............"""
and
long_string = """some
very
long
string
............"""
or even just
long_string = """
some
very
long
string
............"""
are not the same.
There is a textwrap.dedent function in standard library to deal with this, though working with it is out of question's scope.
You can, as well, use \n inside a string, residing on single line:
long_string = "some \nvery \nlong \nstring \n............"
Also, if you don't need any linefeeds (i.e. newlines) in your string, you can use \ inside regular string:
long_string = "some \
very \
long \
string \
............"
Most previous answers are correct and my answer is very similar to aaronasterling, you could also do 3 single quotations
s1='''some very long string............'''