MoonPoint Support Logo


Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals

Advanced Search
Sun Mon Tue Wed Thu Fri Sat
28 29 30  
Jul Aug Sep
Oct Nov Dec

Tue, Apr 11, 2017 8:37 pm

Writing to a CSV file with Python

I need to track the status of firewall rule requests and provide a status report every Monday. The requests are managed through a website where I can see the list of uncompleted requests and their status, but the site doesn't give me a count of the number of requests in each of the states that a request can be in, which can be "Pending Approval", "Modified", "Pending Removal" "On Hold", "Clarification Required", "Waiting Implementation", or "Waiting Removal". To obtain the count of the number of requests in each state, I download the webpage showing the requests and their status to my laptop and then run a Python script to count the queued items. But I also want to see how the numbers are changing over time, so I modified the script to write the counts to a comma-separated values (CSV) file that I can open in a spreadsheet program, such as Microsoft Excel.

To work with comma-separated value (CSV) files in a Python script, include the command import csv in the script. I also include the os.path module to use for testing whether a file already exists that will hold the data, the re module to perform regular expression parsing of lines in the downloaded webpage, the sys module for checking the command line arguments to the script, and the datetime module for determining the current date and formatting it in YYY-MM-DD format, where "YYYY" is the year, "MM" the month, and "DD" the day of the month, as explained in Python - Checking times and dates, for writing the date to the output file.

[ More Info ]

[/languages/python] permanent link

Fri, Apr 07, 2017 3:14 pm

Counting SQLite records based on a specified date

I track work requests in an SQlite database. For each request in a table named "Tasks" in the database, there is a column holding the date the request was approved. Every week I need to include the number of requests approved that week in a weekly report. I can do that with the Structured Query Language (SQL) command SELECT COUNT(*) FROM Tasks WHERE Approved >= date(CURRENT_DATE,"-7 day"). I have a Python script that queries the SQL database to count the requests approved within the last week, but sometimes I want to determine the number of requests approved since a particular date. So I modified the script to accept a date provided as an argument on the command line and, if a date is specified, to determine the number of entries where the approval date matches the specified date or is later than the specified date.

[ More Info ]

[/languages/python] permanent link

Wed, Apr 05, 2017 10:47 pm

Counting queued items with a Python script

I need to review and approve firewall rule requests. I can get a list of those pending approval, ones requesting a modification to existing rules, those pending removal because they've expired (all rules must be reviewed on at least a yearly basis), ones on hold, ones that are in a "clarification required" state due to a question about the rules being requested, those approved for implementation, but not yet implemented, and those awaiting removal. But the page that displays the requests in each category doesn't give me a count of the number in each category, so I wrote a Python script that will read the webpage containing that information that I've downloaded, parse the HTML code for the page and give me a count of the number of requests in each category.

The script is named . The name of the HTML file that was downloaded should be provided on the command line. E.g.:

$ ./ ~/Documents/Work/queued/Request.html
Request Status

Pending Approval:       69
Modified:               36
Pending Removal         43
On Hold:                0
Clarification Required: 28
Waiting Implementation: 26
Waiting Removal:        12

Total requiring review: 176

[ More Info ]

[/languages/python] permanent link

Tue, Feb 21, 2017 9:09 pm

Viewing the list of IP address ranges for AmazonAWS

If you need to create firewall rules for outgoing connectivity to Amazon Web Services (AWS) addresses, you can obtain a complete listing of the IP address ranges that Amazon uses in JavaScript Object Notation (JSON) format from AWS IP Ranges Are Now Available in JSON Format - the download URL for the file is

I downloaded the IP address range file from using cURL, which is present on Apple OS X/macOS systems and is available for Linux and many other operating systems, including Microsoft Windows - see Releases and Downloads.

$ curl -o ip-ranges.json
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   99k  100   99k    0     0   191k      0 --:--:-- --:--:-- --:--:--  191k

The following Python script can be used to extract just the Classless Inter-Domain Routing (CIDR) address blocks, i.e., the ip_prefix information from the JSON data stored in the ip-ranges.json file downloaded from the Amazon website and display it.

import json

data = json.load(open('ip-ranges.json'))
for d in data["prefixes"]:
    print d["ip_prefix"]

[ More Info ]

[/languages/python] permanent link

Sat, Feb 11, 2017 10:27 pm

Using Python to query an SQLite database and return a count of records

I have an SQLite database that I use to track work requests on my MacBook Pro running OS X El Capitan (10.11.6) . Every week I need to determine the number of change requests I've reviewed and approved in the last week. I have been running SQL commands from a text file using sqlite3, but I wanted to start using a Python script, instead, since I want to everntually perform some additional queries as well and display the results as a web page. Initially, I had some problems displaying the record count, but found I could use cursor.fetchone() to display the count. The script I currently have consists of the following code:


import sqlite3
conn = sqlite3.connect('/Users/jasmith1/Documents/Work/CRQ/CRQ.db')
cursor = conn.cursor()

sql = 'SELECT COUNT(*) FROM Tasks WHERE Approved >= date(CURRENT_DATE,"-7 day")'
result = cursor.fetchone()
numApproved = result[0]
print numApproved

The first step I need to take is to import the sqlite3 module for use in Python. I then need to create a connection object for the database which I assign to conn. After that I need to create a cursor instance that I will use to execute an SQL statement. I assign the SQL statement to the variable sql (the name is arbitrary) and then execute the statement with cursor.execute(sql). The variable result is then assigned the results of cursor.fechone(). If I printed the contents of the result variable, I would see (5,), if the count was 5, since the result is a tuple with one element, but I want to see just the number, so I set numApproved to be result[0]. Instead of setting numApproved to result[0] and printing numApproved, I could skip a step and use the following two lines after sursor.execute(sql):

(numApproved,) = cursor.fetchone()
print numApproved

If I make the script excecutable with chmod 755 and then run the script, I will then see just the number of reccords approved within the last week:

$ chmod 755
$ ./


  1. sqlite3 — DB-API 2.0 interface for SQLite databases
    Python documentation

[/languages/python] permanent link

Sun, Nov 13, 2016 10:25 pm

Python script to create a zip file containing all files in a directory

The Python code below will create a zip file from the files and any subdirectories within a specified directory. I.e., it will recursively compress the files within a directory.


# Name:
# Version: 1.0
# Created: 2016-11-13
# Last modified: 2016-11-13
# Purpose: Creates a zip file given a directory where the files to be zipped
# are stored and the name of the output file. Don't include the .zip extension
# when specifying the zip file name.
# Usage: output_filename dir_name
# Note: if the output file name and directory are not specified on the 
# command line, the script will prompt for them.

import sys, shutil

if len(sys.argv) == 1:
   dir_name = raw_input("Directory name: ")
   output_filename = raw_input("Zip file name: ")
elif len(sys.argv) == 3:
   output_filename = sys.argv[1]
   dir_name = sys.argv[2]
   print "Incorrect number of arguments! Usage: output_filename dir_name"

shutil.make_archive(output_filename, 'zip', dir_name)

The script takes two arguments: the output file name and the directory to be compressed. The .zip extension should not be included with the output file name; it will automatically be appended to the output file name. If no arguments are specified on the command line, the script will prompt for them. E.g., if I wanted to create a zip file named from the contents of the folder Example, I could use the command python test Example or, if the script is made executable on a Linux or Apple OS X system with chmod u+x, then ./ test Example can be used.

[/languages/python] permanent link

Fri, Oct 21, 2016 11:29 pm

Python script to extract a column from an Excel spreadsheet

I use the Python xlrd module to extract a column from an Excel spreadsheet. I've been using a Python script, to pull the data from a set column in an .xls spreadsheet, Directory.xls, and store it in a text file. I decided to make the script more generic, so that I can extract the data from a spreadsheet I specify when I run the script rather than a set spreadsheet and to have the specific column and output file name be variable as well. The new script is The Python xlrd module will need to be installed for the script to work; xlrd can be downloaded from one of the links below:

Python Software Foundation
MoonPoint Support (version 1.0.0)

If no arguments are provided on the command line, the script will prompt for the name of the spreadsheet file from which the data should be extracted, the column number containing the data to be extracted (A is treated as column 1, B as column 2, etc.), and the name of the output file to hold the extracted data.

[ More Info ]

[/languages/python/excel] permanent link

Wed, Sep 07, 2016 9:23 pm

Unable to open X server error when attempting to run Python script

When I attempted to execute a Python script today on a CentOS Linux system from a shell prompt with ./, I received the following error message:

import: unable to open X server `' @ error/import.c/ImportImageCommand/369.

The first line of code I had in the file I was attempting to run was as follows:

import json

It has been quite a while since I created a Python script, but the line looked fine to me and I couldn't understand why the JSON library wasn't being imported, but, instead, the import command seemed to be producing an error message. I believe that library has been available for use with Python since Python 2.6 and the system on which I was attempting to execute the script had Python 2.7.5 on it (you can find the version from a shell prompt by issuing the command python --version).

Eventually I realized, I had forgotten to put the shebang line as the first line in the file. The shebang line, which starts with #! tells the shell, in this case the Bash shell, which program to use to interpret the rest of the file. When I put the following line as the first line in the file, that fixed the problem.


I could have run the script successfully without that line, if I used python But when I attempted to execute the script from the command line without making it an argument to the python command, the shell was interpreting the code, rather than Python.

[/languages/python] permanent link

Wed, Jul 13, 2016 10:39 pm

Python control flow using comparators and logical operators

In Python, you can use the followng six comparators to determine if one value is or isn't equal to another or whether the value is less than, less than or equal to, greater than, or greater than or equal to another.

==Equal to
!=Not equal to
<Less than
<=Less than or equal to
>greater than
>=greater than or equal to

[ More Info ]

[/languages/python] permanent link

Wed, Jun 22, 2016 11:15 pm

Python - Checking times and dates

In Python, you can use a function called to retrieve the current date and time. First you need to import the datetime library so that it can be used in the script, which can be done with from datetime import datetime. You can then display the current date and time with print E.g.:

$ python
Python 2.7.10 (default, Jul 14 2015, 19:46:27) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> print
2016-06-22 12:34:06.719688

You can also use

>>> print
2016-06-22 14:31:26.736321

The time is in hours, minutes, seconds, and fractions of a second.

[ More Info ]

[/languages/python] permanent link

Once You Know, You Newegg AliExpress by

Shop Amazon Local - Subscribe to Deals in Your Neighborhood

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo