0

I am trying to convert a json data set file into csv. I am really new to python, and have been looking on the forums and cannot seem to resolve my issues. I have attached the json data url link in below along with my code. Thanks in advance!

https://data.ny.gov/api/views/nqur-w4p7/rows.json?accessType=DOWNLOAD

import json
import csv

    inputFile = ("rows.json?accessType=DOWNLOAD", "r")
    data = json.load(inputFile)

    with open("Data.csv","wb") as csvfile:
      csv_writer = csv.DictWriter(csvfile,delimiter=",", fieldnames=["data", "new_york_state_average_gal", "albany_average_gal", "binghamton_average_gal", "bu\
    ffalo_average_gal", "nassau_average_gal", "new_york_city_average_gal", "rochester_average_gal", "utica_average_gal"])
      csv_writer.writerheader()
      csv_writer.writerows(data)

Here is the error I am getting:

  File "ChangeDataType.py", line 5, in <module>
    data = json.load(inputFile)
  File "/usr/lib64/python3.4/json/__init__.py", line 265, in load
    return loads(fp.read(),
AttributeError: 'tuple' object has no attribute 'read'
3
  • 3
    You're missing the word open... Commented Nov 30, 2017 at 17:33
  • Change it to data = json.load(open(inputFile)) Commented Nov 30, 2017 at 17:34
  • @DustinSmith that should be a separate question. Commented Nov 30, 2017 at 17:37

1 Answer 1

0

Your error happens because you made a tuple:

inputFile = ("rows.json?accessType=DOWNLOAD", "r")

And you're trying to use json.load in that tuple. Since json.load works only on files, you need to call the open function:

inputFile = open("rows.json?accessType=DOWNLOAD", "r")

The "r" part indicates you're opening the file for reading.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.