A Python script that reads a SQL file, executes BigQuery and saves the csv

Works with python 3.6.1.

bq_runnner.py


import argparse
import pandas as pd


def run_query(sql_file):
    with open(sql_file, 'r') as f:
        query = f.read()
    
    project_id = 'YOUR_PROJECT_ID'
    df = pd.read_gbq(query, project_id=project_id, dialect='standard')
    return df


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('sql_file')
    parser.add_argument('--output_csv', default='output.csv')

    args = parser.parse_args()

    df = run_query(args.sql_file)
    df.to_csv(args.output_csv, index=False)

How to use

$ python bq_runnner.py YOUR_QUERY.sql

You can name the csv file as you like.

Required library

requirements.txt


pandas==0.25.3
pandas-gbq==0.13.0

Recommended Posts

A Python script that reads a SQL file, executes BigQuery and saves the csv
A python script that imports a dated csv file into BigQuery as a time partition table
The one that divides the csv file, reads it, and processes it in parallel
A script that retrieves tweets with Python, saves them in an external file, and performs morphological analysis.
A python script that draws a band diagram from the VASP output file EIGENVAL
Python script to create a JSON file from a CSV file
A memo that implements the job of loading a GCS file into BigQuery in Python
[Python] A notebook that translates and downloads the ipynb file on GitHub into Japanese.
[Python] Read the csv file and display the figure with matplotlib
Creating a Python script that supports the e-Stat API (ver.2)
A shell script that just emails the SQL execution result
A python script that converts Oracle Database data to csv
A Python script that compares the contents of two directories
A script that opens the URLs written in CSV in order and takes a full screen screenshot
A Python program that collects tweets containing specific keywords daily and saves them in csv
Python CSV file reading and writing
Create a shell script to run the python file multiple times
A script that returns 0, 1 attached to the first Python prime number
A python script that deletes ._DS_Store and ._ * files created on Mac
Read and format a csv file mixed with comma tabs with Python pandas
Extract lines that match the conditions from a text file with python
Run the Python interpreter in a script
Python script that outputs all records of Oracle table to CSV file
[Python] A program that rounds the score
I made a program in Python that reads CSV data of FX and creates a large amount of chart images
Various ways to read the last line of a csv file in Python
Create a Python image in Django without a dummy image file and test the image upload
[Python, PyPDF2] A script that divides a spread PDF into two left and right
Python --Read data from a numeric data file and find the multiple regression line.
[Python] A program that finds the minimum and maximum values without using methods
[Python] A program that calculates the number of updates of the highest and lowest records
Draw a line / scatter plot on the CSV file (2 columns) with python matplotlib
How to read a CSV file with Python 2/3
A nice nimporter that connects nim and python
ffmpeg-Build a python environment and split the video
I tried reading a CSV file using Python
"Python Kit" that calls a Python script from Swift
A discussion of the strengths and weaknesses of Python
Script python file
A python script that generates a sample dataset for checking the operation of a classification tree
A Python script that crawls RSS in Azure Status and posts it to Hipchat
A python script that gets the number of jobs for a specified condition from indeed.com
A script that combines your favorite python modules and binaries into one Lambda Layer
A program that summarizes the transaction history csv data of SBI SECURITIES stocks [Python3]
A note that runs an external program in Python and parses the resulting line
[Python] A program that counts the number of valleys
Template of python script to read the contents of the file
Try creating a compressed file using Python and zlib
I tried to touch the CSV file with Python
Read the csv file and display it in the browser
A memorandum to run a python script in a bat file
What's in that variable (when running a Python script)
Python program that looks for the same file name
PHP and Python samples that hit the ChatWork API
A memo that I touched the Datastore with python
Output the output result of sklearn.metrics.classification_report as a CSV file
[Python] A program that compares the positions of kangaroos.
[Python] Start a batch file from Python and pass variables.
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
A script that detects file updates and automatically compiles Coffee and Sass or reloads your browser
I wrote a Python script that exports all my posts using the Qiita API v2