Python str object has no attribute read

2024/11/17 21:39:51

Python 3.3.2 import json & urllib.request

Json

[{"link":"www.google.com","orderid":"100000222"},
{"link":"www.google.com","orderid":"100000222"},
{"link":"www.google.com","orderid":"100000222"}]

print(response.info())

Date: Sun, 20 Oct 2013 07:06:51 GMT
Server: Apache
X-Powered-By: PHP/5.4.12
Content-Length: 145
Connection: close
Content-Type: application/json

Codes

url = "http://www.Link.com"request = urllib.request.Request(url)request.add_header('User-Agent','Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')request.add_header('Content-Type','application/json')response = urllib.request.urlopen(request)decodedRes = response.read().decode('utf-8')json_object = json.load(decodedRes)

The following are my codes Error

Traceback (most recent call last):File "C:\Users\Jonathan\Desktop\python.py", line 57, in <module>checkLink()File "C:\Users\Jonathan\Desktop\python.py", line 50, in checkLinkjson_object = json.load(decodedRes)File "C:\Python33\lib\json\__init__.py", line 271, in loadreturn loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'
>>> .

Any idea how i can fix this issue?

Answer

Use json.loads instead of json.load.

json.loads(decodedRes)
  • json.load accept file-like object.

>>> import json
>>> json.load('{"a": 1}')
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "C:\Python27\lib\json\__init__.py", line 286, in loadreturn loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'
>>> json.loads('{"a": 1}')
{u'a': 1}

Alternatively you can pass response object to json.load:

## decodedRes = response.read().decode('utf-8')
json_object = json.load(response)
https://en.xdnf.cn/q/71137.html

Related Q&A

Efficient upsert of pandas dataframe to MS SQL Server using pyodbc

Im trying to upsert a pandas dataframe to a MS SQL Server using pyodbc. Ive used a similar approach before to do straight inserts, but the solution Ive tried this time is incredibly slow. Is there a mo…

Comparison on the basis of min function

How exactly does the min function work for lists in python ?For example,num = [1,2,3,4,[1,2,3]]num2 = [1,2,3,4,5]min(num,num2) gives num2 as the result. Is the comparison value based or length based ?

Python Pandas rolling aggregate a column of lists

I have a simple dataframe df with a column of lists lists. I would like to generate an additional column based on lists.The df looks like:import pandas as pd lists={1:[[1]],2:[[1,2,3]],3:[[2,9,7,9]],4:…

Easy way of overriding default methods in custom Python classes?

I have a class called Cell:class Cell:def __init__(self, value, color, size):self._value = valueself._color = colorself._size = size# and other methods...Cell._value will store a string, integer, etc. …

Return first non NaN value in python list

What would be the best way to return the first non nan value from this list?testList = [nan, nan, 5.5, 5.0, 5.0, 5.5, 6.0, 6.5]edit:nan is a float

How to subplot pie chart in plotly?

How can I subplot pie1 in fig, so it be located at the first position. this is how I am doing it but it doesnt work out import pandas as pdimport numpy as npimport seaborn as snsimport plotly.offline a…

Example of use \G in negative variable-length lookbehinds to limit how far back the lookbehind goes

In the pypi page of the awesome regex module (https://pypi.python.org/pypi/regex) it is stated that \G can be used "in negative variable-length lookbehinds to limit how far back the lookbehind goe…

Regex with lookbehind not working using re.match

The following python code:import reline="http://google.com" procLine = re.match(r(?<=http).*, line) if procLine.group() == "":print(line + ": did not match regex") els…

testing python multiprocessing pool code with nose

I am trying to write tests with nose that get set up with something calculated using multiprocessing.I have this directory structure:code/tests/tests.pytests.py looks like this:import multiprocessing a…

Python verify url goes to a page

I have a list of urls (1000+) which have been stored for over a year now. I want to run through and verify them all to see if they still exist. What is the best / quickest way to check them all and re…