Python - File operations

From PeformIQ Upgrade
Revision as of 08:56, 16 April 2008 by PeterHarding (talk | contribs)
Jump to navigation Jump to search

Examples

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 'FacilityName,QuotedFacilityName,MachineLabel,MachineId,QuotedMachineId'

while True:
   line = ifh.readline()

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

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

   (facility_name, rest) = description.split(';')

   print "%s,%s,%s,%s,%s" % (facility_name, urllib.quote(facility_name), description, machine_ref, urllib.quote(machine_ref))

ifh.close()