SQL to Googlesheets using Python - python

I am trying to connect to a SQL server using Python and then dump the data to google sheets.
import pypyodbc as odbc
import pandas as pd
from google_apis import create_service
when I sun this code block I get an error message
line 1 ModuleNotFound
I have also tried
from Google import Create_Service
import pypyodbc as odbc
import pandas as pd
Can any one help me figure out why.
PLease and thank you.

Related

sqlalchemy and sqlalchemy-clickhouse import error

I'm trying to import sqlalchemy library
import pandas as pd
import sqlalchemy as sa
engine = sa.create_engine('postgresql://{login}:{password}#database.org:5432/database'.format(login = 'login', password='password'))
engine_ch = sa.create_engine('clickhouse://database#ip')
but this ends up with an error
ImportError: cannot import name '_literal_as_label_reference'
I've checked this post, installed the right versions of both libraries, there was no errors during installation, but the import error remains
sqlalchemy version is 1.3.24, sqlalchemy-clickhouse version is 0.1.5.post0

Retrieve the last n days from MongoDB file using Python

I have a code below which connects to a MongoDB database and selects the specified JSON file, flattens it and exports it as a CSV.
So my problem is some of the JSON files in the MongoDB databases are absolutely huge with thousands of rows, so what I am trying to do is filter table down so that I only bring in data from the last 7 days.
from pymongo import MongoClient
import pandas as pd
import os, uuid, sys
import collections
from azure.storage.filedatalake import DataLakeServiceClient
from azure.core._match_conditions import MatchConditions
from azure.storage.filedatalake._models import ContentSettings
from pandas import json_normalize
mongo_client = MongoClient("connstring")
db = mongo_client.nhdb
table = db.Report
document = table.find()
mongo_docs = list(document)
mongo_docs = json_normalize(mongo_docs)
mongo_docs.to_csv("Report.csv", sep = ",", index=False)
Any help will be much appreciated.
Note: I know a way to do it in Azure Data Factory using the expression below, however, I am not sure how to go about it in Python
{"createdDatetime":{$gt: ISODate("#{adddays(utcnow(),-7)}")}}
In python create a datetime object to use as a filter; for example this shows the last 7 days:
from datetime import datetime, timedelta
document = table.find({'createdDatetime': {'$gt': datetime.utcnow() - timedelta(days=7)}})

FXCM Connection Error - Cannot connect to server

I am trying to connect to FXCM throuhg an api and am constantly getting an error:
|ERROR|2020-01-11 20:42:41,825|Socket returns an error: ('Connection aborted.', OSError("(10054, 'WSAECONNRESET')")).
The code is :
import fxcmpy
import pandas as pd
from pandas import datetime
from pandas import DataFrame as df
import matplotlib
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime
from datetime import date
import numpy as np
TOKEN = "hidden"
con = fxcmpy.fxcmpy(access_token=TOKEN, log_level='error')
I have been using this for a while now but error suddenly showed up today. How can i fix this?
I got the same problem. I got an email from api#fxcm.com below.
"We did a release on demo on 1/12 to improve the Rest API.
With that said, Our REST API wrapper fxcmpy has been updated, you need to install the latest fxcmpy version at 1.2.6.
Here is the link where you can find the library: https://pypi.org/project/fxcmpy/#files
Please have in mind that just with pip install fxcmpy it might not work as it won’t update the library, please use below command."
pip install –U fxcmpy

pypyodbc and pandas printing Chinese characters on a Mac

I am trying to query a MS SQL Server database from a Mac using pandas and pypyodbc. My columns names are returned in Chinese characters. This does not happen when running the code from a Windows-based machine. I tried setting the display encoding properties, but that does not work. It doesn't appear to just be a display issue because I cannot reference the columns such as data['col'] as I get a KeyError: 'col'
import pandas as pd
import pypyodbc
import sys
pd.options.display.encoding = sys.stdout.encoding
connection = pypyodbc.connect('Driver={ODBC Driver 13 for SQL Server};'
'Server=server;'
'Database=database;'
'uid=username;'
'pwd=pw')
data = pd.read_sql("""SELECT * FROM dbo.Table""",con=connection)
print data

PYthon 3.3 - api bufferapp. Cant update

i want to publish an update on bufferapp.com with python.
I installed the python buffer modul: https://github.com/bufferapp/buffer-python
I managed to connect to their api with python3.
But if i use the update.publish() command it gives me the following error:
nameerror: "update" is not defined.
Im using this code:
from pprint import pprint as pp
from colorama import Fore
from buffpy.models.update import Update
from buffpy.managers.profiles import Profiles
from buffpy.managers.updates import Updates
from buffpy.api import API
token = 'awesome_token'
api = API(client_id='client_id',
client_secret='client_secret',
access_token=token)
# publish now
update.publish()
What am i doing wrong?
Thank you for your help.

Categories