Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 31st, 2012, 12:26 PM
stevexu stevexu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 4 stevexu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old October 31st, 2012, 02:42 PM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,759 Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 3 h 38 m 3 sec
Reputation Power: 1568
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
  1.  
  2. # code starts here
  3. import ftplib
  4. import os, os.path, time
  5. import socket
  6. import cx_Oracle
  7. import csv
  8. import shutil
  9. import decimal
  10.  
  11. num_rows = sum(1 for line in open('Out.txt'))
  12. print num_rows
  13. basedNum_rows=5
  14. listNo=num_rows/basedNum_rows
  15. print listNo
  16. csvdata=[]
  17. csvdata1=[]
  18. csvdata2=[]
  19. csvdata3=[]
  20. csvdata4=[]
  21. csvdata5=[]
  22. i=1
  23. j=0
  24. #row=0
  25. with open('Out.txt') as textfile:
  26.     csvreader = csv.reader(textfile)
  27.     csvreader.next()
  28.     #for i in range(1,listNo):
  29.         #for index in range(len(csvreader)):
  30.     for row in csvreader:
  31.         if row[3]<>"0.00000000":
  32.             if not ((row[2]).endswith("America/Denver 2011")):
  33.                 if not ((row[2]).endswith("America/Denver 2012")):
  34.                     #print i
  35.                     # j<=5
  36.                     if j>basedNum_rows*(i-1) and j<=basedNum_rows*(i):
  37.                         print basedNum_rows*i
  38.                         #print basedNum_rows*(i+1)
  39.                         #print row
  40.                         csvdata.append(row)
  41.                         #j +=1
  42.                         print 'First j is ' + str(j)
  43.                     # j>5 and j<=10   
  44.                     if j>basedNum_rows*i and j<=basedNum_rows*(i+1):
  45.                         print basedNum_rows*(i+1)
  46.                         #print basedNum_rows*(i+1)
  47.                         #print row
  48.                         csvdata1.append(row)
  49.                         #j +=1
  50.                         print 'Second j is ' + str(j)
  51.                     if j>basedNum_rows*(i+1) and j<=basedNum_rows*(i+2):
  52.                         print basedNum_rows*(i+2)
  53.                         #print basedNum_rows*(i+1)
  54.                         #print row
  55.                         csvdata2.append(row)
  56.                         #j +=1
  57.                         print 'Third j is ' + str(j)
  58.                     if j>basedNum_rows*(i+2) and j<=basedNum_rows*(i+3):
  59.                         print basedNum_rows*(i+3)
  60.                         #print basedNum_rows*(i+1)
  61.                         #print row
  62.                         csvdata3.append(row)
  63.                         #j +=1
  64.                         print 'Four j is ' + str(j)
  65.                     if j>basedNum_rows*(i+3) and j<=basedNum_rows*(i+4):
  66.                         print basedNum_rows*(i+4)
  67.                         #print basedNum_rows*(i+1)
  68.                         #print row
  69.                         csvdata4.append(row)
  70.                         #j +=1
  71.                         print 'Five j is ' + str(j)
  72.                     j +=1
  73.                
  74.    
  75.     #print 'i is ' + str(i)
  76. for x in csvdata:
  77.     print 'First is: '
  78.     print x
  79. for x in csvdata1:
  80.     print 'Second is: '
  81.     print x
  82. for x in csvdata2:
  83.     print 'Third is: '
  84.     print x
  85. for x in csvdata3:
  86.     print 'Four is: '
  87.     print x
  88. for x in csvdata4:
  89.     print 'Five is: '
  90.     print x
  91.  
  92. # code ends here
  93.  


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.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

Reply With Quote
  #3  
Old October 31st, 2012, 02:58 PM
stevexu stevexu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 4 stevexu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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
  1.  
  2. # code starts here
  3. import ftplib
  4. import os, os.path, time
  5. import socket
  6. import cx_Oracle
  7. import csv
  8. import shutil
  9. import decimal
  10.  
  11. num_rows = sum(1 for line in open('Out.txt'))
  12. print num_rows
  13. basedNum_rows=5
  14. listNo=num_rows/basedNum_rows
  15. print listNo
  16. csvdata=[]
  17. csvdata1=[]
  18. csvdata2=[]
  19. csvdata3=[]
  20. csvdata4=[]
  21. csvdata5=[]
  22. i=1
  23. j=0
  24. #row=0
  25. with open('Out.txt') as textfile:
  26.     csvreader = csv.reader(textfile)
  27.     csvreader.next()
  28.     #for i in range(1,listNo):
  29.         #for index in range(len(csvreader)):
  30.     for row in csvreader:
  31.         if row[3]<>"0.00000000":
  32.             if not ((row[2]).endswith("America/Denver 2011")):
  33.                 if not ((row[2]).endswith("America/Denver 2012")):
  34.                     #print i
  35.                     # j<=5
  36.                     if j>basedNum_rows*(i-1) and j<=basedNum_rows*(i):
  37.                         print basedNum_rows*i
  38.                         #print basedNum_rows*(i+1)
  39.                         #print row
  40.                         csvdata.append(row)
  41.                         #j +=1
  42.                         print 'First j is ' + str(j)
  43.                     # j>5 and j<=10   
  44.                     if j>basedNum_rows*i and j<=basedNum_rows*(i+1):
  45.                         print basedNum_rows*(i+1)
  46.                         #print basedNum_rows*(i+1)
  47.                         #print row
  48.                         csvdata1.append(row)
  49.                         #j +=1
  50.                         print 'Second j is ' + str(j)
  51.                     if j>basedNum_rows*(i+1) and j<=basedNum_rows*(i+2):
  52.                         print basedNum_rows*(i+2)
  53.                         #print basedNum_rows*(i+1)
  54.                         #print row
  55.                         csvdata2.append(row)
  56.                         #j +=1
  57.                         print 'Third j is ' + str(j)
  58.                     if j>basedNum_rows*(i+2) and j<=basedNum_rows*(i+3):
  59.                         print basedNum_rows*(i+3)
  60.                         #print basedNum_rows*(i+1)
  61.                         #print row
  62.                         csvdata3.append(row)
  63.                         #j +=1
  64.                         print 'Four j is ' + str(j)
  65.                     if j>basedNum_rows*(i+3) and j<=basedNum_rows*(i+4):
  66.                         print basedNum_rows*(i+4)
  67.                         #print basedNum_rows*(i+1)
  68.                         #print row
  69.                         csvdata4.append(row)
  70.                         #j +=1
  71.                         print 'Five j is ' + str(j)
  72.                     j +=1
  73.                
  74.    
  75.     #print 'i is ' + str(i)
  76. for x in csvdata:
  77.     print 'First is: '
  78.     print x
  79. for x in csvdata1:
  80.     print 'Second is: '
  81.     print x
  82. for x in csvdata2:
  83.     print 'Third is: '
  84.     print x
  85. for x in csvdata3:
  86.     print 'Four is: '
  87.     print x
  88. for x in csvdata4:
  89.     print 'Five is: '
  90.     print x
  91.  
  92. # code ends here
  93.  


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.

Reply With Quote
  #4  
Old October 31st, 2012, 03:27 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,384 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 13 h 48 m 49 sec
Reputation Power: 383
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).

Reply With Quote
  #5  
Old October 31st, 2012, 07:12 PM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,759 Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 3 h 38 m 3 sec
Reputation Power: 1568
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.
Attached Files
File Type: txt Have-a-Heart Trappist.txt (805 Bytes, 33 views)

Reply With Quote
  #6  
Old November 1st, 2012, 09:42 AM
stevexu stevexu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 4 stevexu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #7  
Old November 1st, 2012, 03:38 PM
stevexu stevexu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 4 stevexu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #8  
Old November 1st, 2012, 04:13 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,384 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 13 h 48 m 49 sec
Reputation Power: 383
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > How to create simplify my code to dynamically create if conditions?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap