I'm trying to write some basic code to retrieve the list of workspaces and write the response to a file. I thought that this could be dumped to a JSON file?
Thanks for any help/suggestions.
I have taken the sample .py file and reworked it to look like this -
# Install the smartsheet sdk with the command: pip install smartsheet-python-sdk
import smartsheet
import logging
import os.path
import json
# TODO: Set your API access token here, or leave as None and set as environment variable "SMARTSHEET_ACCESS_TOKEN"
access_token = None
print("Starting ...")
# Initialize client
smart = smartsheet.Smartsheet(access_token)
# Make sure we don't miss any error
smart.errors_as_exceptions(True)
# Log all calls
logging.basicConfig(filename='rwsheet.log', level=logging.INFO)
response = smart.Workspaces.list_workspaces(include_all=True)
workspaces = response.data
with open('data.json', 'w') as outfile:
json.dump(workspaces, outfile)
print("Done")
I'm not sure what issue you are facing, but I think you would have an issue with the json.dump(workspaces, outfile) line as the results of that workspaces variable is a list that you would need to iterate through to get through the data. Using that variable will just print out the pointer with something like this:
[<smartsheet.models.workspace.Workspace object at 0x10382a4e0>]
To work around this you would need to loop over the results of the variable and print them each out to a file. I found this post here about printing output to a file. The answer gives three approaches and I was able to get each of them to work with a loop iterating over the results.
One example:
import smartsheet
smar_client = smartsheet.Smartsheet(<ACCESS_TOKEN>)
response = smar_client.Workspaces.list_workspaces(include_all=True)
workspaces = response.data
with open('workspaces.json', 'w') as f:
for workspace in workspaces:
print(workspace, file=f)
Running this gave me a workspaces.json file in the same directory I ran my script from with the list of workspace objects.
Related
I'm using the code below to pull specific tweets and save to a .json file format and keep getting the following error:
IOError: [Errno 2] No such file or directory 'data/greenroof.json'
The code in question:
import json
import tweepy
API_KEY = "key_here"
API_SECRET = "secret here"
TOKEN_KEY = "t_key_here"
TOKEN_SECRET = "t_secret_here"
auth = tweepy.OAuthHandler(API_KEY, API_SECRET)
auth.set_access_token(TOKEN_KEY, TOKEN_SECRET)
api = tweepy.API(auth)
query = '#greenroof'
cursor = tweepy.Cursor(api.search, q=query, lang="en")
for page in cursor.pages():
tweets = []
for item in page:
tweets.append(item._json)
with open('data/greenroof.json', 'wb') as outfile:
json.dump(tweets, outfile)
Please forgive the spacing of the above code, I cant appear to get it to present accurately.
I've used the mkdir data request on the command line, ensuring the path for said directory matches the location of script.
The error suggest that the file doesn't exist.
Any help or pointers greatly appreciated.
Oh, and go easy on me folks, total newbie to code & python.
if the data directory is in your current working directory, make sure to add a ./ to the beginning of the path, signifying the current working directory.
i.e.
with open('./data/greenroof.json', 'wb') as outfile:
How can i resolve problem in Python?
Read config.json file located in :
/data/python/config.json
make a GET request to the URL under ‘url’ key and add the first 15 characters
to a key name ‘content’ in the json file.
config.json:
{"url": "https://www.google.com"}
config.json after code run:
{"url": "https://www.google.com", "content": ""}
Where should be the first 15 characters from the response.
Welcome to StackOverflow. This is not a site to ask people to write a code.
Next time asking a question, please read this article. You are to show your efforts and show the problems you encounter.
This is the code, that will help you to get what you want
Note: you will need to install requests library before running the code. pip install requests
import json
import requests
with open("/data/python/config.json", "r") as f:
config = json.load(f)
result = requests.get(config['url'])
config['content'] = result.text[:15]
with open("config.json", "w") as f:
json.dump(config, f)
It does simple things:
Loads config.json from json to python dict
Gets url value from it
Sends request using requests library
Adds content slice of length 15 to a dict with a key content
Saves updated dictionary to config.json
Brand new to stack and python; hopefully someone wiser than myself can help. I have searched up and down and can't seem to find an actual answer to this, apologies if there is an exact answer and I've missed it :( (the few that I've found are either old or don't seem to work).
Closest I've found is
Best way to retrieve variable values from a text file?
Alas, imp seems to be depreciated and tried figuring out importlib but little above my current brain to figure out how to adapt it as errors throw up left and right on me.
This is very close to what I want and could potentially work if someone can help update with new methods, alas still doesn't have how to overwrite the old variable.
= - - Scenario - - =
I would like to create a preferences file (let's call it settings.txt or settings.py: doesn't need to be cross-compatible with other languages, but some reason I'd prefer txt - any preference/standards coders can impart would be appreciated?).
\\\ settings.txt\
water_type = "Fresh"\
measurement = "Metric"\
colour = "Blue"\
location = "Bottom"\
...
I am creating a script main_menu.py which will read variables in settings.txt and write to this file if changes are 'saved'
ie.
"select water type:"
Fresh
Salt
if water_type is the same as settings.txt, do nothing,
if water_type different, overwrite the variable in the settings.txt file
Other scripts down the line will also read and write to this settings file.
I've seen:
from settings import *
Which seems to work for reading the file if I go the settings.py path but still leaves me on how do I overwrite this.
also open to any better/standard/ideas you guys can think of.
Appreciate any help on this!
Here are some suggestions that may help you:
Use a json file:
settings.json
{
"water_type": "Fresh",
"measurement": "Metric",
"colour": "Blue",
"location": "Bottom",
...
}
then in python:
import json
# Load data from the json file
with open("settings.json", "r") as f:
x = json.load(f) # x is a python dictionary in this case
# Change water_type in x
x["water_type"] = "Salt"
# Save changes
with open("settings.json", "w") as f:
json.dump(x, f, indent=4)
Use a yaml file: (edit: you will need to install pyyaml)
settings.yaml
water_type: Fresh
measurement: Metric
colour: Blue
location: Bottom
...
then in python:
import yaml
# Load data from the yaml file
with open("settings.yaml", "r") as f:
x = yaml.load(f, Loader=yaml.FullLoader) # x is a python dictionary in this case
# Change water_type in x
x["water_type"] = "Salt"
# Save changes
with open("settings.yaml", "w") as f:
yaml.dump(x, f)
Use a INI file:
settings.ini
[Preferences]
water_type=Fresh
measurement=Metric
colour=Blue
location=Bottom
...
then in python:
import configparser
# Load data from the ini file
config = configparser.ConfigParser()
config.read('settings.ini')
# Change water_type in config
config["Preferences"]["water_type"] = "Salt"
# Save changes
with open("settings.ini", "w") as f:
config.write(f)
For .py config files, it's usually static options or settings.
Ex.
# config.py
STRINGTOWRITE01 = "Hello, "
STRINGTOWRITE02 = "World!"
LINEENDING = "\n"
It would be hard to save changes made to the settings in such a format.
I'd recommend a JSON file.
Ex. settings.json
{
"MainSettings": {
"StringToWrite": "Hello, World!"
}
}
To read the settings from this file into a Python Dictionary, you can use this bit of code.
import json # Import pythons JSON library
JSON_FILE = open('settings.json','r').read() # Open the file with read permissions, then read it.
JSON_DATA = json.loads(JSON_FILE) # load the raw text from the file into a json object or dictionary
print(JSON_DATA["MainSettings"]["StringToWrite"]) # Access the 'StringToWrite' variable, just as you would with a dictionary.
To write to the settings.json file you can use this bit of code
import json # import pythons json lib
JSON_FILE = open('settings.json','r').read() # Open the file with read permissions, then read it.
JSON_DATA = json.loads(JSON_FILE) # load the data into a json object or dictionary
print(JSON_DATA["MainSettings"]["StringToWrite"]) # Print out the StringToWrite "variable"
JSON_DATA["MainSettings"]["StringToWrite"] = "Goodnight!" # Change the StringToWrite
JSON_DUMP = json.dumps(JSON_DATA) # Turn the json object or dictionary back into a regular string
JSON_FILE = open('settings.json','w') # Reopen the file, this time with read and write permissions
JSON_FILE.write(JSON_DUMP) # Update our settings file, by overwriting our previous settings
Now, I've written this so that it is as easy as possible to understand what's going on. There are better ways to do this with Python Functions.
You guys are fast! I'm away from the computer for the weekend but had to log in just to say thanks.
I'll look into these more next week when I'm back at it and have some time to give it the attention needed. A quick glance could be a bit of fun to implement and learn a bit more.
Had to answer as adding comment only is on one of your guys solutions and wanted to give a blanket thanks to all!
Cheers
Here's a python library if you choose to do it this way.
If not this is also a good resource.
Creating a preferences file example
Writing preferences to file from python file
import json
# Data to be written
dictionary ={
"name" : "sathiyajith",
"rollno" : 56,
"cgpa" : 8.6,
"phonenumber" : "9976770500"
}
# Serializing json
json_object = json.dumps(dictionary, indent = 4)
# Writing to sample.json
with open("sample.json", "w") as outfile:
outfile.write(json_object)
Reading preferences from .json file in Python
import json
# open and read file content
with open('sample.json') as json_file:
data = json.load(json_file)
# print json file
print(data)
I found (and tried successfully) to pull the user emails using terminal but was wondering if anyone knew how to do it from python? My program needs to the file daily and I'm trying to automate it.
Here's what I have
import firebase_admin
from firebase_admin import credentials
cred = credentials.Certificate("cred file")
firebase_admin.initialize_app(cred)
emails = (firebase auth:export account_file.csv)
then I try and read it, but the auth:export line is where it breaks giving me a syntax error
emails = (firebase auth:export account_file.csv) is not a correct sintax in python.
Haven't used it, but this function seems to list firebase users:
firebase_admin.auth.list_users(page_token=None, max_results=1000, app=None)
Check documentation here.
Once you have all your users toghether in some variable like firebase_users, you'll need to export them to .csv.
import csv
with open('firebase_users.csv', 'w', newline='') as csvfile:
fieldnames = ['first_name', 'mail']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
for user in firebase_users:
writer.writerow(user)
Also check here for python csv package.
As I said, haven't tried it. I'll do it later to refresh any possible mistake.
I want to have a function which can save a page from the web into a designated path using urllib2.
Problem with urllib is that it doesn't check for Error 404, but unfortunately urllib2 doesn't have such a function although it can check for http errors.
How can i make a function to save the file permanently to a path?
def save(url,path):
g=urllib2.urlopen(url)
*do something to save g to 'path'*
Just use .read() to get the contents and write it to a file path.
def save(url,path):
g = urllib2.urlopen(url)
with open(path, "w") as fH:
fH.write(g.read())