Skip to content

Instantly share code, notes, and snippets.

Embed
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: make-url.py [-h] [--url "BASEURL"] CSVFILE

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

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. "http://domain.com/?"

Example ouput based on test.csv

URL
http://domain.com/?first-name=Gil&last-name=Creque&id=1&place=Melbourne%2C%20FL
http://domain.com/?first-name=Joe&last-name=Smith&id=2&place=Eau%20Gallie%2C%20FL
#!/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. "http://domain.com/?"')
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 = data.next()
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
sys.stdout.write(outputString)
if __name__ == '__main__':
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