Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Make URL field-value pair parameters from a CSV file and append it to a base URL string

This script expects a CSV file headers in the first row.

Help output

usage: [-h] [--url "BASEURL"] CSVFILE

Create URLs from CSV file. Every column will become a parameter in the query

positional arguments:
  CSVFILE               the csv file to extract data from

optional arguments:
  -h, --help            show this help message and exit
  --url "BASEURL", -u "BASEURL"
                        the URL being appened to i.e. ""

Example ouput based on test.csv

#!/usr/bin/env python
import sys
import csv
import re
import urllib
import argparse
def main():
parser = argparse.ArgumentParser(description='Create URLs from CSV file. Every column will become a parameter in the query string.')
parser.add_argument('file', metavar='CSVFILE', help='the csv file to extract data from')
parser.add_argument('--url', '-u', metavar='"BASEURL"', help='the URL being appened to i.e. ""')
args = vars(parser.parse_args())
baseURL = args["url"]
csvFileName = args["file"]
data = ""
outputString = "URL\n"
#check if csv file exists and can be opened
with open(csvFileName, 'rb') as csvFile:
data = csv.reader(csvFile, delimiter=',')
headers =
rowString = ""
for row in data:
if baseURL:
rowString = baseURL
for columnNmber, column in enumerate(row):
if (columnNmber != 0):
rowString += "&"
rowString += urllib.quote(headers[columnNmber]) + "=" + urllib.quote(column)
outputString += rowString + "\n"
#send outputString to console
if __name__ == '__main__':
first-name last-name id place
Gil Creque 1 Melbourne, FL
Joe Smith 2 Eau Gallie, FL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment