Difference between revisions of "Python - File operations"

From PeformIQ Upgrade
Jump to navigation Jump to search
 
Line 12: Line 12:
ifh = open('Machines.txt', 'r')
ifh = open('Machines.txt', 'r')


print 'FacilityName,QuotedFacilityName,MachineLabel,MachineId,QuotedMachineId'
print 'Name,QuotedName,Label,LoginId,QuotedLoginId'


while True:
while True:
Line 21: Line 21:
   line = line[:-1]
   line = line[:-1]


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


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


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


ifh.close()
ifh.close()

Latest revision as of 11:13, 2 April 2009

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 '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))

ifh.close()