How can I use json module in google app engine? [duplicate] - python

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can I parse JSON in Google App Engine?
json module was added in python 2.6 but GAE supports 2.5 only.
How can I use it there?

Well you wotn be able to use the standard python library, you will have to use some third party lib such as
jyson or
simplejson
At least simplejson works on python 2.4

You can try using yaml module (which is included in GAE) as JSON parser. YAML in recent version is superset of JSON.
>>> import yaml
>>> yaml.load('[false, null, {"x": 1}]')
[False, None, {'x': 1}]

You can try using django.utils.simplejson

Related

markdown2 module - how do I convert a a markdown file from within a Python script?

I am using markdown2 module (found here: https://github.com/trentm/python-markdown2) The module is great but documentation is lacking.
I attempted something simple like converting a file using a Python script. I wanted to then use this to convert a batch of files. But even the first part is hitting a dead end. My code:
from markdown2 import Markdown
markdowner = Markdown()
markdowner.convert("file.md", "file.htm")
Could someone show me how?
NVM, I got the answer (and not from the repository mentioned above). Here's a simple Pythonic two-liner:
from markdown2 import Markdown
with open("file.htm", 'w') as output_file: output_file.write(Markdown().convert(open("file.md").read()))

How to pass data(Args,parameter,list ..etc) from python2 to python3 in memory? [duplicate]

This question already has answers here:
What is the preferred way of passing data between two applications on the same system?
(6 answers)
Closed 5 years ago.
Owing to some reason,I must use "win32com" this module to output my Dataframe from another appication (But this module seems to be only in python2).
However , I want to do some calculate in python3 with this Dataframe(output by python2).
How can I send the Dataframe from python2 to python3 in memory? (Except to output the data to the file)
Supplementary explanation 1:
My os is Win10 64bit (both contain python2 and python3)
In short , I want to know how can I pass the Data(produced by python2) to python3.
Supplementary explanation 2:
I have a A python script and it need to run in python2.
A python script will generated some data(maybe json , dataframe ..)
And then I want pass this data to B python script
B python script must run in python3.
My os is win10 64bits(both have python2 and 3).
I am a new to python , I have tried "out the data to the file ,then B.py read the file". However this I/O way is too slow , so I want pass data in memory , how can I do that?
(My English is not very good, please entertain me )
JSON is a convenient option.
import json
read the docs for specifics and examples
https://docs.python.org/2/library/json.html
https://docs.python.org/3/library/json.html

Getting unexpected output while creating a client for Google’s Maps API using httplib module in python 2.7 [duplicate]

This question already has answers here:
Google Geocoding v2 API stopped working suddenly
(2 answers)
Closed 8 years ago.
import httplib
try:
import json
except ImportError:
import simplejson as json
path = ('/maps/geo?q=207+N.+Defiance+St%2C+Archbold%2C+OH''&output=json&oe=utf8')
connection = httplib.HTTPConnection('maps.google.com')
connection.request('GET', path)
rawreply = connection.getresponse().read()
reply = json.loads(rawreply)
print(reply)
on executing it i'm not getting a desired output instead of it i'm getting:
{u'Status': {u'code': 610, u'request': u'geocode'}}
If anyone knows the solution kindly help me.
You need to move from version 2 to version 3 of the API. The old API which you are using was retired in March. See this page on how to upgrade.

How can I paste some string to the active window in Python? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I copy a string to the clipboard on Windows using Python?
Can someone make me an example or explain to me how can I paste something to the active window with Python?
It is easiest if you use the SendKeys package. You can find a Windows installer for various Python versions here.
The simplest use case, sending plain text, is very simple:
import SendKeys
SendKeys.SendKeys("Hello world")
You can do all sorts of nifty things using key-codes to represent for unprintable characters:
import SendKeys
SendKeys.SendKeys("""
{LWIN}
{PAUSE .25}
r
Notepad.exe{ENTER}
{PAUSE 1}
Hello{SPACE}World!
{PAUSE 1}
%{F4}
n
""")
Read the documentation for full details.
If for whatever reason you don't want to introduce a dependency on a non-standard library package, you can do the same thing using COM:
import win32api
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.Run("calc")
win32api.Sleep(100)
shell.AppActivate("Calculator")
win32api.Sleep(100)
shell.SendKeys("1{+}")
win32api.Sleep(500)
shell.SendKeys("2")
win32api.Sleep(500)
shell.SendKeys("~") # ~ is the same as {ENTER}
win32api.Sleep(500)
shell.SendKeys("*3")
win32api.Sleep(500)
shell.SendKeys("~")
win32api.Sleep(2500)

How can I parse JSON in Google App Engine?

I'd like to parse a JSON string into an object under Google App Engine (python). What do you recommend? Something to encode/stringify would be nice too. Is what you recommend built in, or a library that I have to include in my app? Is it secure? Thanks.
Consider using Django's json lib, which is included with GAE.
from django.utils import simplejson as json
# load the object from a string
obj = json.loads( string )
The link above has examples of Django's serializer, and here's the link for simplejson's documentation.
If you're looking at storing Python class instances or objects (as opposed to compositions of lists, strings, numbers, and dictionaries), you probably want to look at pickle.
Incidentally, to get Django 1.0 (instead of Django 0.96) running on GAE, you can use the following call in your main.py, per this article:
from google.appengine.dist import use_library
use_library('django', '1.0')
Edit: Native JSON support in Google App Engine 1.6.0 with Python 2.7
As of Google App Engine 1.6.0, you can use the Python 2.7 runtime by adding runtime: python27 in app.yaml, and then you can import the native JSON library with import json.
Google App Engine now supports python 2.7. If using python 2.7, you can do the following:
import json
structured_dictionary = json.loads(string_received)
Include the simplejson library with your app?
This is an old question, but I thought I'd give an updated, more detailed answer. For those landing here now, you are almost certainly using python 2.6 or greater, so you can use the built-in json module for Python 2 (or for Python 3, since Google recently added support for Python 3 on GAE). Importing is as easy as import json. Here are some examples of how to use the json module:
import json
# parse json_string into a dict
json_string = '{"key_one": "value_one", "key_two": 1234}'
json_dict = json.loads(json_string)
# json_dict: {u'key_two': 1234, u'key_one': u'value_one'}
# generate json from a dict
json_dict = {'key': 'value', 'key_two': 1234, 'key_three': True}
json_string = json.dumps(json_dict)
# json_string: '{"key_two": 1234, "key": "value", "key_three": true}'
If you are using an older version of python, stick to #Brian M. Hunt's answer.
Again, here is the doc page for the json module for Python 2, and here it is for Python 3.
If you're using Python2.6 or greater, I've used with success the built-in json.load function. Otherwise, simplejson works on 2.4 without dependencies.
Look at the python section of json.org. The standard library support for JSON started at python 2.6, which I believe is newer than what the app engine provides. Maybe one of the other options listed?

Categories

Resources