Home Verilog Digital Design Digital Basics Python RF Basics

Legal Disclaimer

Chip Designing for ASIC/ FPGA Design engineers and Students
FULLCHIPDESIGN
Digital-logic Design...  Dream for many students… start learning front-end…
Custom Search

Feedback ? Send it to admin@fullchipdesign.com or join me at fullchip@gmail.com

Python File read operations Class & global var File write op's Glob module Conditional stms String 2 hex UNIX in Python Diamond Pattern TypeError - arg TyprError - str TypeError - strconv AttributeError Function call Read bin files

Legal Disclaimer

Topics @TYH :- 4G LTE Tutorial, GVIM editor, Smart-Phone, Cloud Computing
Previous Next
Verilog Tutorial.
Digital Basics Tutorial.

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, -114)

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 - 1)):

                self.idata_f = self.idata_f - 4096    

             if (self.qdata_f > 4096/2 - 1):

                self.qdata_f = self.qdata_f - 4096

            

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