Categories
Python Answers

How to import a CSV file into a sqlite3 database table using Python?

Sometimes, we want to import a CSV file into a sqlite3 database table using Python.

In this article, we’ll look at how to import a CSV file into a sqlite3 database table using Python.

How to import a CSV file into a sqlite3 database table using Python?

To import a CSV file into a sqlite3 database table using Python, w ecan use the execute method to run SQL queries.

And we use the csv module to read the CSV.

For instance, we write

import csv, sqlite3

con = sqlite3.connect(":memory:") 
cur = con.cursor()
cur.execute("CREATE TABLE t (col1, col2);") 

with open('data.csv','r') as fin:
    dr = csv.DictReader(fin)
    to_db = [(i['col1'], i['col2']) for i in dr]

cur.executemany("INSERT INTO t (col1, col2) VALUES (?, ?);", to_db)
con.commit()
con.close()

to open the SQLite database with

con = sqlite3.connect(":memory:") 

Then we open the cursor with con.cursor.

And then we run the create table query with

cur.execute("CREATE TABLE t (col1, col2);") 

Next, we open the csv file with open.

We read the rows with

dr = csv.DictReader(fin)

Then we create the to_db list that has tuples with the values we want to insert into the database from the csv.

Next, we run the insert query to insert all the items with

cur.executemany("INSERT INTO t (col1, col2) VALUES (?, ?);", to_db)
con.commit()

Finally, we close the database with con.close.

Conclusion

To import a CSV file into a sqlite3 database table using Python, w ecan use the execute method to run SQL queries.

And we use the csv module to read the CSV.

Categories
Python Answers

How to see the entire HTTP request that’s being sent with Python?

Sometimes, we want to see the entire HTTP request that’s being sent with Python.

In this article, we’ll look at how to see the entire HTTP request that’s being sent with Python.

How to see the entire HTTP request that’s being sent with Python?

To see the entire HTTP request that’s being sent with Python, we can use the requests module.

For instance, we write

r = requests.get('https://api.github.com', auth=('user', 'pass'))

to make a GET request.

We can get the request content from r.request.

And we can get the request headers with r.request.headers.

And we can get the request body data with r.request.data.

Conclusion

To see the entire HTTP request that’s being sent with Python, we can use the requests module.

Categories
Python Answers

How to parse a YAML file in Python?

Sometimes, we want to parse a YAML file in Python.

In this article, we’ll look at how to parse a YAML file in Python.

How to parse a YAML file in Python?

To parse a YAML file in Python, we can use the yaml module.

To install it, we run

pip install pyyaml

Then we use it by writing

import yaml

with open("example.yaml", "r") as stream:
    try:
        print(yaml.safe_load(stream))
    except yaml.YAMLError as exc:
        print(exc)

to open the example.yaml file.

And then we call yaml.safe_load method to read the open stream.

If there’s an error with parsing the YAML code, then a yaml.YAMLError is raised and we catch it with the except block.

Conclusion

To parse a YAML file in Python, we can use the yaml module.

Categories
Python Answers

How to call a Python function from Node.js?

Sometimes, we want to call a Python function from Node.js.

In this article, we’ll look at how to call a Python function from Node.js.

How to call a Python function from Node.js?

To call a Python function from Node.js, we can use the child_process module.

For instance, we write

const { spawn } = require("child_process");
//...
const pythonProcess = spawn("python", ["path/to/script.py", arg1, arg2]);

to call spawn to run python with the command line arguments in an array.

Conclusion

To call a Python function from Node.js, we can use the child_process module.

Categories
Python Answers

How to use PhantomJS in Python?

Sometimes, we want to use PhantomJS in Python.

In this article, we’ll look at how to use PhantomJS in Python.

How to use PhantomJS in Python?

To use PhantomJS in Python, we can use the PhantomJS driver.

To use it, we run

npm -g install phantomjs-prebuilt

to install PhantomJS.

Then we use it by writing

from selenium import webdriver

driver = webdriver.PhantomJS() 
driver.set_window_size(1024, 768)
driver.get('https://google.com/')
driver.save_screenshot('screen.png') 
sbtn = driver.find_element_by_css_selector('button')
sbtn.click()

to create the PhantomJS driver with

driver = webdriver.PhantomJS() 

We open the webpage with get.

Then we call save_screenshot to save the screenshot of the webpage.

And we call find_element_by_css_selector to find the element with the CSS given selector.

And we click the element with click.

Conclusion

To use PhantomJS in Python, we can use the PhantomJS driver.