#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep 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
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    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:
     
    # 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.
    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 Short Understanding the C/C++ Preprocessor
    Taming Python A 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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep 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?

    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:
     
    # 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.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    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']
    >>>
    Last edited by b49P23TIvg; October 31st, 2012 at 07:38 PM. Reason: Remove extra line (an input echo).
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    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
    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 Short Understanding the C/C++ Preprocessor
    Taming Python A 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
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0
    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
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0
    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
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    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.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo