Python script - control flow statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python script - control flow statements
# 1  
Old 06-01-2009
Python script - control flow statements

Hi guys.I'm just beginner of python.
I'm just trying to do some analysis on simple input file.
it has 6 columns and i want to consider k,l and m,n if i and j are +
after that checking which value is greater or lower in k,l and m,n
I have included logic header just to explain what I was trying to do
plz take a look at my python script.Plz Suggest me any ideas

Inputfile:


columns k,l,i,m,n,j
30,50,+,100,200,+
210,250,+,100,200,+
150,180,+,100,200,+
80,150,+,100,200,+
150,250,+,100,200,+
100,200,-,100,200,-
200,100,+,200,100,-
100,250,-,50,60,-

logic:

while i and j columns are +,+

if k<m and l<m (30<100 and 50<100)
print all the columns plus "good"
elif k>n and l>n (210>100 and 250>200)
print all the columns with "good"
elif k>m and l<n
print all... with "bad"
elif k>m and l>n
print all.....with "average"
elif k<m and l<n
print all......with "avergae"

outputfile:

columns k,l,i,m,n,j
30,50,+,100,200,+,good
210,250,+,100,200,+,good
150,180,+,100,200,+,bad
80,150,+,100,200,+,average
150,250,+,100,200,+,averge

Python script_raw

Quote:
d = {}

for line in open ("filename"): #open file
line = line.strip().split() #splitting file into columns
d.setdefault (line[-1],[-3],[])
d[-1][-3].append(line[0][1][3][4])

for i,j,k,l,m,n in d.iteritems ():


while i = +, j = +:

if (k<m, l<m):
print k, '\t', l, '\t', i, '\t', m, '\t', n,'\t', j, '\t', 'Good'

elif (k>n, l>n):
print k, '\t', l, '\t', i, '\t', m, '\t', n,'\t', j, '\t', 'Good'

elif (k>m, l<n):
print k, '\t', l, '\t', i, '\t', m, '\t', n,'\t', j, '\t', 'Bad'

elif (k<m, l<n):
print k, '\t', l, '\t', i, '\t', m, '\t', n,'\t', j, '\t', 'Average'

elif (k>m, l>n):
print k, '\t', l, '\t', i, '\t', m, '\t', n,'\t', j, '\t', 'Average'

break
# 2  
Old 06-01-2009
put your Python code in code tags, not quote tags.
Code:
for line in open("file"):
    oline=line.strip()
    if j =="+" and i=="+":
        k,l,i,m,n,j=oline.split(",")
        if k<m and  l<m:
            print oline,",good"
        elif .....

if you are a beginner, you should go read the documentation. Read the tutorial and get familiarize with Python. From the way you write the code, i believe you haven't even look at the basics. (there is no need for open and close brackets in Python, except for functions/tuples)
# 3  
Old 06-01-2009
Hey thanx for the suggestion.
The tutorial link is really good.
I'm getting the following error when i tried the following sode
Could you plz tell me why
Am I indented wronglySmilie

Traceback (most recent call last):
File "filehandler.py", line 11, in <module>
if j=="+" and i=="+":
NameError: name 'j' is not defined
Press any key to continue . . .

Code:
d = {}

for line in open ("filename.csv"):
    oline=line.strip()
    if j=="+" and i=="+":
        k,l,i,m,n,j=oline.split(",")
        if k<m and  l<m:
            print oline,",Good"
        elif k>n and l>n:
            print oline,",Good"
        elif k>m and l<n:
            print oline,",IBad"
        elif k<m and l<n:
            print oline,",Avg"
        elif k>m and l>n:
            print oline,",Avg"

else:
    print ' not found'

# 4  
Old 06-01-2009
please look at the error carefully, its says "j" is not defined. that's all there is. very straightforward.
Code:
    if j=="+" and i=="+":
        k,l,i,m,n,j=oline.split(",")

the above is the offending error. I leave it to you to try finding it yourself. the split should be done before the checking for j value. simple? pls read the tutorial before posting another query.
# 5  
Old 06-01-2009
Hey Got it!
Thanx alot for not telling and directing in the right way
I got the result
I jumbled the lines...........................

Code:
 k,l,i,m,n,j=oline.split(",")
 if j=="+" and i=="+":

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Flow control state changed in server logs

Hi, We observe below logs from switch - the database servers rebooted becaause they couldn't do I/O on vfiler -Any pointers looking at below logs please? Switch logs: 2016 Apr 30 07:41:16.729 EAG-ECOM-POD111GPU-SWF1 %ETHPORT-5-IF_DOWN_LINK_FAILURE: Interface Ethernet152/1/8 is down (Link... (0 Replies)
Discussion started by: admin_db
0 Replies

2. Programming

Python Conditional Statements Based on Success of Last Command

In shell scripting, I can create a conditional statement based on the success or failure (exit status)of a command such as: pinger() { ping -c 2 $remote_host >/dev/null 2>&1 ping_stat=$? } pinger if ]; then echo "blahblahblah" exit 0 fi how is this done using Python using... (3 Replies)
Discussion started by: metallica1973
3 Replies

3. Shell Programming and Scripting

Help with control flow in a Bash script

In my bash script I want to say "if argument 2 is anything except x, y or z, than echo this" (x y and z being words). So my script looks like this: if ] then echo "unrecognized input: $2" fi This usually works but than I also want to say "if argument 2 IS x, y, or z, but argument 4 is... (4 Replies)
Discussion started by: Jrodicon
4 Replies

4. Shell Programming and Scripting

Backup script using Korn Flow Control

I am wiping off the dust to my shell scipting days and had this question: I have this script that I have created, and cannot figure out where my flow control issue is within this script. #!/bin/ksh #Basic script to backup server #Home directories to the external ... (2 Replies)
Discussion started by: metallica1973
2 Replies

5. Shell Programming and Scripting

Flow Control in CSH

hi , I am new to scripting, i have a doubt can any one pls solve it for me the code is not working set users = (user1 user2 user3) echo The users are echo $users echo Enter the USER NAME set USER_NAME = $< set i = 1; for ( i = 1; i <= $#users; i++ ) if ( $USER_NAME == $users )... (1 Reply)
Discussion started by: Manju87
1 Replies

6. Shell Programming and Scripting

Help with make sure shell script execute instruction in flow

Hi, I want to write a shell script to make sure all the instruction is executive in flow. eg. I want my shell script to run finish this two progress first: ./program input_file_1.txt > input_file_1.txt.out & ./program input_file_2.txt > input_file_2.txt.out & After then, only run the... (1 Reply)
Discussion started by: edge_diners
1 Replies

7. UNIX for Advanced & Expert Users

Script should flow after ftp --Urgent

Hi everyone, The script actually does the ftp and gets the file to the local system. I want to do some manipulations for that file , But after doing ftp , script is not proceding and just a prompt is displayed . .... ftp code here...... .................... ............... echo "FTP... (4 Replies)
Discussion started by: kaaakrishna
4 Replies

8. IP Networking

Disabling 802.3x flow control

I have a server I would like to disable 802.3x flow control on. The host is Linux (CentOS 4.4 x86_64 w/ 2.6.9-42.0.3.EL kernel,) and I'm using the ns83820 driver for the ethernet interface in question. I've tried looking at the driver parameters (modinfo ns83820) and using ethtool (ethtool -a... (0 Replies)
Discussion started by: LivinFree
0 Replies

9. Programming

dilemma in control flow

hello im facing a queer problem when i execute the foll code in unix # include <stdio.h> # include <unistd.h> main(int argc,char *argv) { FILE *fp = fopen("/ras/chirag/fifotest/file.fifo","a"); int i=1; fprintf(fp,argv); printf("I SLEEP"); system("date"); for (i=0;i<50;i++)... (2 Replies)
Discussion started by: tej.buch
2 Replies

10. UNIX for Dummies Questions & Answers

how to assign correct control flow?

how can i create a script with a correct control flow/loop(?) to provide output from 3 files? file1 one two three file2 yellow red orange file3 banana apples cantaloupes output file: one (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question