write csv row (from within for loop) out to csv file without using python csv module
Question: **My goal is to avoid importing the csv module

I am working on a script that runs through an extremely large csv file and
selectively writes rows to a new csv file.

I have the following two lines:

    
    
    with open(sys.argv[1]) as ifile, open(sys.argv[2], mode = 'w') as ofile:
        for row in ifile: 
    

and then this, a few nested-if statements down:

    
    
    line = list(ifile)[row]
    ofile.write(line)
    

I know that isn't right--I took a stab at it and was hoping someone here could
shed some light on how to correctly go about this. The essence of this
question is how to reference the row that I am in so that I can write it out
to the new csv file using 'ofile'. Please let me know if any further
clarifications are necessary. Thanks!

EDIT: Full Code included in pastebin link - <http://pastebin.com/a0jx85xR>


Answer: You're quite close. This is all you have to do:

    
    
    with open(sys.argv[1]) as ifile, open(sys.argv[2], mode = 'w') as ofile:
        for row in ifile:
    
        #...
        #You've defined some_condition to be met (you will have to replace this for yourself)
        #E.g.: the number of entries in each row is greater than 5:
            if len([term for term in row.split('#') if term.strip() != '']) > 5:
                ofile.write(row)
    

UPDATE:

To answer the OP's question about splitting lines:

you split a line in Python by supplying the delimiting character. Since this
is a CSV file, you split the line by the `,`. Example:

If this is a line (a string):

    
    
    0, 1, 2, 3, 4, 5
    

If you apply:

    
    
    line.split(',')
    

You'll obtain _a list_ :

    
    
    ['0', '1', '2', '3', '4', '5']
    

UPDATE 2:

    
    
    import sys
    
    if __name__ == '__main__':
        ticker = sys.argv[3]
        allTypes = bool(int(sys.argv[4])) #argv[4] is a string, you have to convert it to an int, then to a bool
    
        with open(sys.argv[1]) as ifile, open(sys.argv[2], mode = 'w') as ofile:
            all_timestamps = [] #this is an empty list
            n_rows = 0
            for row in ifile:
                #This splits the line into constituent terms as described earlier
                #SAMPLE LINE:
                #A,1,12884902522,B,B,4900,AAIR,0.1046,28800,390,B,AARCA,
                #After applying this bit of code, the line should be split into this:
                #['A', '1', '12884902522', 'B', 'B', '4900', 'AAIR', '0.1046', '28800', '390', 'B', 'AARCA']
                #NOW, you can make comparisons against those terms. :)
    
                terms = [term for term in row.split(',') if term.strip() != '']
                current_timestamp = int(terms[2])
    
                #compare the current against the previous
                #starting from row 2: (index 1)
                if n_rows > 1:
                    #Python uses circular indices, hence: -1 means the value at the last index
                    #That is, the previous time_stamp. Now perform the comparison and do something if that criterion is met:
                    if current_timestamp - all_timestamp[-1] >= 0:
                        pass #the pass keyword means to do nothing. You'll have to replace it with whatever code you want
    
                #increment n_rows every time:
                n_rows += 1
    
                #always append the current timestamp to all the time_stamps
                all_timestamps.append(current_timestamp)
    
    
                if (terms[6] == ticker):
                    # add something to make sure chronological order hasn't been broken
                    if (allTypes == 1):
                        ofile.write(row)
                #I don't know if this was a bad indent of not, but you should know
                #where this goes
                elif (terms[0] == "A" or terms[0] == "M" or terms[0] == "D"):
                    print row
                    ofile.write(row)
    

My original conjecture was correct. **You weren't splitting the row into the
the CSV components.** Hence, when you were making comparisons on the rows, you
weren't getting the correct results - thus, you weren't getting any output.
This ought to work now (given slight modifications as per your objectives). :)

