TypeError: expected string or buffer in Google App Engines Python

2024/9/8 11:01:18

I want to show the content of an object using the following code:

def get(self):url="https://www.googleapis.com/language/translate/v2?key=MY-BILLING-KEY&q=hello&source=en&target=ja"data = urllib2.urlopen(url)parse_data = json.load(data)parsed_data = parse_data['data']['translations']// This command is okself.response.out.write("<br>")// This command shows above errorself.response.out.write(str(json.loads(parsed_data[u'data'][u'translations'][u'translatedText'])))

But the error

TypeError: expected string or buffer

appears as a result of the line:

self.response.out.write(str(json.loads(parsed_data[u'data'][u'translations'][u'translatedText'])))

or

self.response.out.write(json.loads(parsed_data[u'data'][u'translations'][u'translatedText']))

UPDATE (fix):

I needed to convert from string to JSON object:

    # Convert to Stringparsed_data = json.dumps(parsed_data)# Convert to JSON Objectjson_object = json.loads(parsed_data)# Parse JSON ObjecttranslatedObject = json_object[0]['translatedText']# Output to page, by using HTMLself.response.out.write(translatedObject)
Answer
parse_data = json.load(data)
parsed_data = parse_data['data']['translations']

Those lines already did the json.load, and extracted 'data' and 'translations'. Then instead of:

self.response.out.write(str(json.loads(parsed_data)[u'data'][u'translations'][u'translatedText']))

you should:

self.response.out.write(str(parsed_data[u'translatedText']))
https://en.xdnf.cn/q/72426.html

Related Q&A

Returning a row from a CSV, if specified value within the row matches condition

Ahoy, Im writing a Python script to filter some large CSV files.I only want to keep rows which meet my criteria.My input is a CSV file in the following formatLocus Total_Depth Average_Depth_sa…

Python multiprocessing pool: dynamically set number of processes during execution of tasks

We submit large CPU intensive jobs in Python 2.7 (that consist of many independent parallel processes) on our development machine which last for days at a time. The responsiveness of the machine slows …

TypeError: cant escape psycopg2.extensions.Binary to binary

I try to store binary file into postgresql through sqlalchemy and file is uploaded from client. A bit google on the error message brings me to this source file:" wrapped object is not bytes or a…

Keras: Cannot Import Name np_utils [duplicate]

This question already has answers here:ImportError: cannot import name np_utils(19 answers)Closed 6 years ago.Im using Python 2.7 and a Jupyter notebook to do some basic machine learning. Im following…

Python 3 string index lookup is O(1)?

Short story:Is Python 3 unicode string lookup O(1) or O(n)?Long story:Index lookup of a character in a C char array is constant time O(1) because we can with certainty jump to a contiguous memory loca…

Using PIL to detect a scan of a blank page

So I often run huge double-sided scan jobs on an unintelligent Canon multifunction, which leaves me with a huge folder of JPEGs. Am I insane to consider using PIL to analyze a folder of images to detec…

Pandas: Filling data for missing dates

Lets say Ive got the following table:ProdID Date Val1 Val2 Val3 Prod1 4/1/2019 1 3 4 Prod1 4/3/2019 2 3 54 Prod1 4/4/2019 3 4 54 Prod2 4/1/2019 1 3 3…

Linear Regression: How to find the distance between the points and the prediction line?

Im looking to find the distance between the points and the prediction line. Ideally I would like the results to be displayed in a new column which contains the distance, called Distance.My Imports:impo…

How to draw a Tetrahedron mesh by matplotlib?

I want to plot a tetrahedron mesh by matplotlib, and the following are a simple tetrahedron mesh: xyz = np.array([[-1,-1,-1],[ 1,-1,-1], [ 1, 1,-1],[-1, 1,-1],[-1,-1, 1],[ 1,-1, 1], [ 1, 1, 1],[-1, 1, …

How to set seaborn jointplot axis to log scale

How to set axis to logarithmic scale in a seaborn jointplot? I cant find any log arguments in seaborn.jointplot Notebook import seaborn as sns import pandas as pddf = pd.read_csv("https://storage…