
Feedback ? Send it to admin@fullchipdesign.com or join me at fullchip@gmail.com
PROGRAM : python_file1.py
USAGE of the PROGRAM
From UNIX command prompt use:
>> python python_file1.py input_vectors.txt
Sample input vector text file
1AF00 F0 0E1 1F4E
RESULT
>> print self.rfile
1af00 f00e1 1f4eee
>> print (self.ln)
19
>> print self.idata, self.qdata
1aa f8e
>> print self.iqdata
(426, -
Advanced Python Program to cover:
¨ Read file
¨ Filter text
¨ Conversion from string to hexadecimal to integer
¨ Conversion from 12 bit hexadecimal value to signed magnitude
¨ File write operations
¨ Conditional statements if else and for.
import sys
import os
import string
class file_op:
def file_read(self, file):
self.file_to_read = file
self.rfile = self.file_to_read.readline()
print self.rfile
# To calculate length
self.ln = len(self.rfile)
print (self.ln)
# Open file for Writing
self.temp_write = open('temp_file.txt' , 'a')
# Strip off white space on end
self.rfile = string.strip(self.rfile)
# Read 12 bit vectors as string from the text vectors file
self.ci = 0
self.idata = 0
self.qdata = 0
for self.ci in range(10, 11, 1):
self.idata = self.rfile[self.ci] + self.rfile[self.ci+1] + self.rfile[self.ci+2]
self.qdata = self.rfile[self.ci+5] + self.rfile[self.ci+6] + self.rfile[self.ci+7]
print self.idata, self.qdata
# Convert String to a Integer Value
self.idata_f = int(self.idata, 16)
self.qdata_f = int(self.qdata, 16)
# Convert hexadecimal value to signed integers
if (self.idata_f > (4096/2 -
self.idata_f = self.idata_f -
if (self.qdata_f > 4096/2 -
self.qdata_f = self.qdata_f -
self.iqdata = self.idata_f, self.qdata_f
print self.iqdata
# Write file
self.temp_write.write(self.iqdata)
# Close file
self.temp_write.close()
return
if len(sys.argv) == 1:
r1 = file_op()
r1.file_read(sys.stdin)
else:
r1 = file_op()
r1.file_read(open(sys.argv[1], 'r'))