The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
How to create simplify my code to dynamically create if conditions?
Discuss How to create simplify my code to dynamically create if conditions? in the Python Programming forum on Dev Shed. How to create simplify my code to dynamically create if conditions? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 31st, 2012, 12:26 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 4
Time spent in forums: 1 h 9 m 35 sec
Reputation Power: 0
|
|
|
How to create simplify my code to dynamically create if conditions?
Hi guys:
I have the following python code. read text file by csv reader, and then append every 5 rows to a list. I don't want to write endless if conditions and delare endless list because I don't know how many lines in text file which comes from our gps dump file each time. Thanks a lot!
# code starts here
import ftplib
import os, os.path, time
import socket
import cx_Oracle
import csv
import shutil
import decimal
num_rows = sum(1 for line in open('Out.txt'))
print num_rows
basedNum_rows=5
listNo=num_rows/basedNum_rows
print listNo
csvdata=[]
csvdata1=[]
csvdata2=[]
csvdata3=[]
csvdata4=[]
csvdata5=[]
i=1
j=0
#row=0
with open('Out.txt') as textfile:
csvreader = csv.reader(textfile)
csvreader.next()
#for i in range(1,listNo):
#for index in range(len(csvreader)):
for row in csvreader:
if row[3]<>"0.00000000":
if not ((row[2]).endswith("America/Denver 2011")):
if not ((row[2]).endswith("America/Denver 2012")):
#print i
# j<=5
if j>basedNum_rows*(i-1) and j<=basedNum_rows*(i):
print basedNum_rows*i
#print basedNum_rows*(i+1)
#print row
csvdata.append(row)
#j +=1
print 'First j is ' + str(j)
# j>5 and j<=10
if j>basedNum_rows*i and j<=basedNum_rows*(i+1):
print basedNum_rows*(i+1)
#print basedNum_rows*(i+1)
#print row
csvdata1.append(row)
#j +=1
print 'Second j is ' + str(j)
if j>basedNum_rows*(i+1) and j<=basedNum_rows*(i+2):
print basedNum_rows*(i+2)
#print basedNum_rows*(i+1)
#print row
csvdata2.append(row)
#j +=1
print 'Third j is ' + str(j)
if j>basedNum_rows*(i+2) and j<=basedNum_rows*(i+3):
print basedNum_rows*(i+3)
#print basedNum_rows*(i+1)
#print row
csvdata3.append(row)
#j +=1
print 'Four j is ' + str(j)
if j>basedNum_rows*(i+3) and j<=basedNum_rows*(i+4):
print basedNum_rows*(i+4)
#print basedNum_rows*(i+1)
#print row
csvdata4.append(row)
#j +=1
print 'Five j is ' + str(j)
j +=1
#print 'i is ' + str(i)
for x in csvdata:
print 'First is: '
print x
for x in csvdata1:
print 'Second is: '
print x
for x in csvdata2:
print 'Third is: '
print x
for x in csvdata3:
print 'Four is: '
print x
for x in csvdata4:
print 'Five is: '
print x
# code ends here
|

October 31st, 2012, 02:42 PM
|
 |
Commie Mutant Traitor
|
|
Join Date: Jun 2004
Location: Norcross, GA (again)
|
|
For future note, you should always use either [code] or [highlight] tags around any code sample you post. This is because the forum software does not by default retain indentation, and without the special tags, too much information is lost, especially when dealing with Python. So that you can see the difference, here is your code with highlighting added:
Python Code:
Original
- Python Code |
|
|
|
# code starts here import ftplib import os, os.path, time import socket import cx_Oracle import csv import shutil import decimal num_rows = sum(1 for line in open('Out.txt')) print num_rows basedNum_rows=5 listNo=num_rows/basedNum_rows print listNo csvdata=[] csvdata1=[] csvdata2=[] csvdata3=[] csvdata4=[] csvdata5=[] i=1 j=0 #row=0 with open('Out.txt') as textfile: csvreader = csv.reader(textfile) csvreader.next() #for i in range(1,listNo): #for index in range(len(csvreader)): for row in csvreader: if row[3]<>"0.00000000": if not ((row[2]).endswith("America/Denver 2011")): if not ((row[2]).endswith("America/Denver 2012")): #print i # j<=5 if j>basedNum_rows*(i-1) and j<=basedNum_rows*(i): print basedNum_rows*i #print basedNum_rows*(i+1) #print row csvdata.append(row) #j +=1 print 'First j is ' + str(j) # j>5 and j<=10 if j>basedNum_rows*i and j<=basedNum_rows*(i+1): print basedNum_rows*(i+1) #print basedNum_rows*(i+1) #print row csvdata1.append(row) #j +=1 print 'Second j is ' + str(j) if j>basedNum_rows*(i+1) and j<=basedNum_rows*(i+2): print basedNum_rows*(i+2) #print basedNum_rows*(i+1) #print row csvdata2.append(row) #j +=1 print 'Third j is ' + str(j) if j>basedNum_rows*(i+2) and j<=basedNum_rows*(i+3): print basedNum_rows*(i+3) #print basedNum_rows*(i+1) #print row csvdata3.append(row) #j +=1 print 'Four j is ' + str(j) if j>basedNum_rows*(i+3) and j<=basedNum_rows*(i+4): print basedNum_rows*(i+4) #print basedNum_rows*(i+1) #print row csvdata4.append(row) #j +=1 print 'Five j is ' + str(j) j +=1 #print 'i is ' + str(i) for x in csvdata: print 'First is: ' print x for x in csvdata1: print 'Second is: ' print x for x in csvdata2: print 'Third is: ' print x for x in csvdata3: print 'Four is: ' print x for x in csvdata4: print 'Five is: ' print x # code ends here
As for the problem you are having, the obvious (if not necessarily best) solution is to nest the data lists inside of a list, and iterate through that master list. I'd say more, but I'm short on time at the moment; I'll get back to this later this evening.
|

October 31st, 2012, 02:58 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 4
Time spent in forums: 1 h 9 m 35 sec
Reputation Power: 0
|
|
Thank Schol-R-LEA for your quick response. Sorry for not post my code properly. I'm starting to learn Python. I don't know how to convert my text file (usually more than 20k lines) to list in list, very much appreciate your help. I'd like to attached my simplified text file (out.txt), please let me know how I can attached a file here?
Quote: | Originally Posted by Schol-R-LEA For future note, you should always use either [code] or [highlight] tags around any code sample you post. This is because the forum software does not by default retain indentation, and without the special tags, too much information is lost, especially when dealing with Python. So that you can see the difference, here is your code with highlighting added:
Python Code:
Original
- Python Code |
|
|
|
# code starts here import ftplib import os, os.path, time import socket import cx_Oracle import csv import shutil import decimal num_rows = sum(1 for line in open('Out.txt')) print num_rows basedNum_rows=5 listNo=num_rows/basedNum_rows print listNo csvdata=[] csvdata1=[] csvdata2=[] csvdata3=[] csvdata4=[] csvdata5=[] i=1 j=0 #row=0 with open('Out.txt') as textfile: csvreader = csv.reader(textfile) csvreader.next() #for i in range(1,listNo): #for index in range(len(csvreader)): for row in csvreader: if row[3]<>"0.00000000": if not ((row[2]).endswith("America/Denver 2011")): if not ((row[2]).endswith("America/Denver 2012")): #print i # j<=5 if j>basedNum_rows*(i-1) and j<=basedNum_rows*(i): print basedNum_rows*i #print basedNum_rows*(i+1) #print row csvdata.append(row) #j +=1 print 'First j is ' + str(j) # j>5 and j<=10 if j>basedNum_rows*i and j<=basedNum_rows*(i+1): print basedNum_rows*(i+1) #print basedNum_rows*(i+1) #print row csvdata1.append(row) #j +=1 print 'Second j is ' + str(j) if j>basedNum_rows*(i+1) and j<=basedNum_rows*(i+2): print basedNum_rows*(i+2) #print basedNum_rows*(i+1) #print row csvdata2.append(row) #j +=1 print 'Third j is ' + str(j) if j>basedNum_rows*(i+2) and j<=basedNum_rows*(i+3): print basedNum_rows*(i+3) #print basedNum_rows*(i+1) #print row csvdata3.append(row) #j +=1 print 'Four j is ' + str(j) if j>basedNum_rows*(i+3) and j<=basedNum_rows*(i+4): print basedNum_rows*(i+4) #print basedNum_rows*(i+1) #print row csvdata4.append(row) #j +=1 print 'Five j is ' + str(j) j +=1 #print 'i is ' + str(i) for x in csvdata: print 'First is: ' print x for x in csvdata1: print 'Second is: ' print x for x in csvdata2: print 'Third is: ' print x for x in csvdata3: print 'Four is: ' print x for x in csvdata4: print 'Five is: ' print x # code ends here
As for the problem you are having, the obvious (if not necessarily best) solution is to nest the data lists inside of a list, and iterate through that master list. I'd say more, but I'm short on time at the moment; I'll get back to this later this evening. |
|

October 31st, 2012, 03:27 PM
|
 |
Contributing User
|
|
|
|
Likewise, I'm out of time. I had thought there was a method in itertools to help, I can't find such. This code enlists every 5th line, of course it could be a cvs reader iterator as you have. uh, I didn't read your post or code. You may have wanted something completely different. Sorry.
Code:
>>> import pprint
>>> with open ('p.py') as inf:
... every_5th_line = [L for (i,L) in enumerate(inf)if not i%5]
...
>>> pprint.pprint(every_5th_line)
['import math\n',
'TAU=2*math.pi # http://www.youtube.com/watch?v=jG7vhMMXagQ\n',
" '''\n",
' self.canvas = tkinter.Canvas(self.tk)\n',
" sin = array.array('f',range(len(self)))\n",
' angle = a*rreciprocal\n',
' def __len__(self):\n',
" coords = array.array('I',(0,)*(2*len(self)))\n",
'\n',
' bufsize=L,\n',
' hf = self.hf\n',
' lfscale = scale/36.0\n',
'\n',
'\n']
>>>
__________________
[code] Code tags[/code] are essential for python code!
Last edited by b49P23TIvg : October 31st, 2012 at 07:38 PM.
Reason: Remove extra line (an input echo).
|

October 31st, 2012, 07:12 PM
|
 |
Commie Mutant Traitor
|
|
Join Date: Jun 2004
Location: Norcross, GA (again)
|
|
Quote: | Originally Posted by stevexu I'd like to attached my simplified text file (out.txt), please let me know how I can attached a file here? |
Ah, it's quite simple, really, but easy to overlook. If you reply to the thread, and scroll down below the editing window, you'll find a button marked 'Manage Attachments'. If you click on that, it should let you browse to the file you want and upload it. To demonstrate this, I've uploaded a more or less randomly selected file off of my system (a beer recipe, to be specific, but the contents don't really matter here) which you should find below this post window.
|

November 1st, 2012, 09:42 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 4
Time spent in forums: 1 h 9 m 35 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Schol-R-LEA Ah, it's quite simple, really, but easy to overlook. If you reply to the thread, and scroll down below the editing window, you'll find a button marked 'Manage Attachments'. If you click on that, it should let you browse to the file you want and upload it. To demonstrate this, I've uploaded a more or less randomly selected file off of my system (a beer recipe, to be specific, but the contents don't really matter here) which you should find below this post window. |
Sorry I cannot find a button marked 'Manage Attachments'?
Thanks
|

November 1st, 2012, 03:38 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 4
Time spent in forums: 1 h 9 m 35 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by stevexu Sorry I cannot find a button marked 'Manage Attachments'?
Thanks |
I found 'Posting rules': 'You may not post attachments', which sounds 
|

November 1st, 2012, 04:13 PM
|
 |
Contributing User
|
|
|
|
|
mail it to me, I'll post it.
b49p23tivg@yahoo.com
oh! but you probably should post in this thread that you sent me mail. I only look at that account by request.
Last edited by b49P23TIvg : November 1st, 2012 at 04:22 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|