user11669928
user11669928

Reputation:

How to save json dumps in google colab using python?

x = json.dumps (....)

how to save x to a json file and store it in google drive or download it to local pc using google colab

currently using this when i am in local pc

 with open('data.json', 'w') as f:
    f.write(x.encode('utf-8'))

Upvotes: 2

Views: 11059

Answers (3)

Alvaro
Alvaro

Reputation: 651

from google.colab import drive
drive.mount('/content/drive')

# open existing text file
with open('/content/drive/My Drive/Sandbox/Sandbox.txt’) as f:
  print(f.read())

# make new JSON file
with open('/content/drive/My Drive/Sandbox/Sandbox_2.json', 'w') as f:
  f.write('{"hello":true}')

From here.

Upvotes: 0

Christopher
Christopher

Reputation: 941

Downloading it to your local drive, reference here

from google.colab import files
import json

x = json.dumps ('test')
y = x.encode('utf-8')
with open('example.txt', 'w') as f:
  f.write(str(y))

files.download('example.txt')

Alternatively, I as previously mentioned in the comment, you can mount your Google drive using the script provided or simply click the mount drive then run the cell provided. After mounting, you can now write to it like a local drive, fyr.

Mount Google Drive

Upvotes: 3

Sukhi
Sukhi

Reputation: 14145

As suggested by Christopher, you can take help of this. The link will help you to eventually mount Google Colab as a local drive. Once this is done, you can easily read-write the file from/to Google Colab. In my opinion, it's worth trying and it should work. Note : I've not tried and tested this solution.

Upvotes: 0

Related Questions