Python - File operations

From PeformIQ Upgrade
Jump to navigation Jump to search


URL Encoding Data

The following example was used to build a data set for LoadRunner testing where some of the data needed to be URL encoded

#!/usr/bin/env python

import urllib

ifh = open('Machines.txt', 'r')

print 'Name,QuotedName,Label,LoginId,QuotedLoginId'

while True:
   line = ifh.readline()

   if not line: break
   line = line[:-1]

   (login_id, description) = line.split(',')

   (label, name, rest) = description.split(';')

   print "%s,%s,%s,%s,%s" % (name, urllib.quote(name), label, login_id, urllib.quote(login_id))