Nested Json Lists formatting incorrectly - python

Ok, so I'm using an API. I'm trying to display a list that is returned by the api. The challenge is that I need to use .json to go through the response, but then it makes the list a json list and looks wrong.
checkList #is the return value
>>> checkList
u'{"list":["ad","ae"]}'
>>> str(checkList.json()['list'])
"[u'ad', u'ae']"
I'm using a python shell. How would I remove the " u' " from each element in the list? Thanks

The issue is not really in removing the u from the start of those strings. The easiest way to do this is to import the json module and call json.dumps(checklist.json()['list']). It will do the right thing for you. The strings the json module returns are unicode objects (and are represented in the repr) as unicode literals. To "remove" them you need to handle the unicode strings better and this is the easiest way that will result in the least hair pulling and most forward compatibility with python 3.

Related

Why do backslashes appear twice in dict? [duplicate]

This question already has an answer here:
Why does printing a tuple (list, dict, etc.) in Python double the backslashes?
(1 answer)
Closed 7 months ago.
When I create a string containing backslashes, they get duplicated:
NOTE : I want to add \ in request because i want to call third party API and they want me to send request with \ in some of their keys.
I have taken reference from this answer Why do backslashes appear twice?, but its working only for string, not for dict.
mystr = {"str": "why\does\it\happen?"}
print(mystr)
output:
{'str': 'why\\does\\it\\happen?'}
here i am attching a screenshot for better understanding.
mystr isn't a str, it's a dict. When you print a dict, it prints the repr of each string inside it, rather than the string itself.
>>> mydict = {"str": "why\does\it\happen?"}
>>> print(mydict)
{'str': 'why\\does\\it\\happen?'}
>>> print(repr(mydict['str']))
'why\\does\\it\\happen?'
>>> print(mydict['str'])
why\does\it\happen?
Note that the repr() includes elements other than the string contents:
The quotes around it (indicating that it's a string)
The contents use backslash-escapes to disambiguate the individual characters. This extends to other "special" characters as well; for example, if this were a multiline string, the repr would show linebreaks as \n within a single line of text. Actual backslash characters are always rendered as \\ so that they can be distinguished from backslashes that are part of other escape sequences.
The key thing to understand is that these extra elements are just the way that the dict is rendered when it is printed. The actual contents of the string inside the dict do not have "doubled backslashes", as you can see when you print mydict['str'].
If you are using this dict to call an API, you should not be using str(mydict) or anything similar; if it's a Python API, you should be able to use mydict itself, and if it's a web API, it should be using something like JSON encoding (json.dumps(mydict)).
I think that to build the printed string of the dict, python call the __repr__ method of object inside it (for the values) instead of the __str__ as you would expect for printing the dict.
It would make sense since dict can contain every type of object not just string so the __repr__| method can be found everywhere (it's included in the base object in python) when the __str__ need to be written.
But it's only a guess, not a definitive answer.

Can valid JSON be invalid Python?

This is a simple questions that is really only a footnote in something I am writing:
Is any valid JSON not also valid Python?
I know the converse is true, i.e. Python data structures and scalars allow a variety of constructs that are not JSON. But for the most part, JSON seems to be a subset of Python syntax for defining (some) data structures.
The obvious stuff is covered. Strings are strings. Ints are ints. JSON "numbers" are read as Python floats (although RFC 8259 does not mandate that interpretation vs. fixed point, for example). Dicts are dicts. Lists are lists.
But maybe something in some obscure corner violates the subset relationship. For example, is there anything in the encoding of Unicode outside the BMP that is directly incompatible? Or maybe within Unicode surrogate pairs?
Or maybe something with numbers where some large number of digits after the decimal would be technically valid JSON but not Python? (I don't think so, but just trying to think of scenarios).
The most obvious thing is that true, false and null don't exist in Python. They are called True, False and None.
In addition, \/ in strings is interpreted as / in json and as \/ in Python:
>>> a = '"\/"'
>>> print(a)
"\/"
>>> print(eval(a))
\/
>>> print(json.loads(a))
/
Yes, you are correct, every valid JSON can be handled in Python. Python is a complete language, and JSON is a way of storing data (serialisation maybe?). Generally a language will support everything a JSON object can represent.
There would be different representation for sure like true in JSON is True in Python.
Since, JSON is way of storing data, and we can also pass it around HTTP requests, which are always processed by some server side language, which is expected to handle the JSON object.

Write Number as string csv python

I'm trying to write a csv file from json data. During that, i want to write '001023472' but its writing as '1023472'. I have searched a lot. But dint find an answer.
The value is of type string before writing. The problem is during writing it into the file.
Thanks in advance.
Convert the number to string with formatting operator; in your case: "%09d" % number.
Use the format builtin or format string method.
>>> format(1023472, '09')
'001023472'
>>> '{:09}'.format(1023472)
'001023472'
If your "number" is actually a string, you can also just left-pad it with '0''s:
>>> format('1023472', '>09')
'001023472'
The Python docs generally eschew % formatting, saying it may go away in the future and is also more finnicky; for new code there is no real reason to use it, especially in 2.7+.

Python array conversion to a string

I am trying to write the contents of a re.findall to a file. I tried
output_file.write (findall(tags_pattern, searchtext))
but I got a type error. How do I convert this to a type that can be written to a file?
Thanks
The easiest way is to JSON-encode it. See the json module.
you have the str(res) and repr(res) function but you could also do ','.join(res)
re.findall returns a list of matches found in searchtext for tags_pattern. Those matches are just strings. The easiest way to convert a list of strings into a single string which can be written to a file is to call str.join on a string representing the separator you want to insert between the strings in the list. For example, you may call '\n'.join(findall(tags_pattern, searchtext)) if you want to store each match on its own line.
The pickle module is built to quickly store Python structures in a file. It is not nearly as portable as JSON or some other serialization format, but depending on your purposes, it may be just enough.
To use pickle:
import re, pickle
r = re.findall(pattern, text)
with open('results.pkl', 'wb') as resultsfile:
pickle.dump(r, resultsfile)
To recover the list, use pickle.load:
with open('results.pkl', 'rb') as resultsfile:
r2 = pickle.load(resultsfile)
I'd be wary of using this in production code, or where you need to transmit the re.findall results to a web client, but for quick testing and local storage, this is probably the easiest.

Python repr function problem

I'm dealing with some text parsing in Python and for that purpose, it's good for me to apply repr() function on each string I'm gonna parse, but after the parsing, I need to convert some parsed substring back to the previous representation, because I want to print them and I'm not able to do this. I thought that str() function should get the string back to the human more readable form. But when I apply str function on that substring, nothing's changed.
As I've said I need to print the string in human readable form, without printing escape sequences like \n, \t etc...
But when I apply repr() to a string and then I want to convert it back, I don't know how, because str() function didn't do it.
So my question is, how to convert the string back into human readable form?
Thanks for every reply.
str() has no effect on objects that are already strings. You need to use eval() to undo a repr() where possible. Try using ast.literal_eval() instead though.

Categories