ASIC/FPGA Design and Verification Out Source Services
Reading from STDIN: python script.
-
Recently I started to study python .
In this site I present many
scripts.
For simple tasks, I use bash and for
more complex ones perl. Since my daughter starts
to study
python , so do
I.
-
This script reads line by line from STDIN and checks if 192 is
found in the beginning of the string. If so an asterisk is printed. If not
a minus is printed.
-
I had some problems using the method
rstrip . From search in the web, I
found that split works better. The code is shown below:
#!/usr/bin/python
#handle regular expression
import re
import sys
for line_i in sys.stdin:
#same as perl chomp
#line.rstrip()
line_a = line_i.splitlines()
line = line_a[0]
ix = line.find('192')
#!= -1 pattern found somewhere in line
#== 0 pattern found in string start
if(ix == 0):
print "* ", line
else:
print "- ", line
|