0

I was wondering if someone could tell me how to write out the header for columns 0 and 3 from the original CSV file to the new CSV file? I'm also curious if anyone has any expereince with pushing to google docs?

**

#!/usr/bin/python

import csv
import re
import sys
import gdata.docs.service


email = "[email protected]" 
password = "password"

#string_1 = ('OneTouch AT')
#string_2 = ('LinkRunner AT')
#string_3 = ('AirCheck')

searched = ['aircheck', 'linkrunner at', 'onetouch at']


def find_group(row):
    """Return the group index of a row
        0 if the row contains searched[0]
        1 if the row contains searched[1]
        etc
        -1 if not found
    """
    for col in row:
        col = col.lower()
        for j, s in enumerate(searched):
            if s in col:
                return j
        return -1

#def does_match(string):
#    stringl = string.lower()
#    return any(s in stringl for s in searched)

#Opens Input file for read and output file to write.
inFile  = open('data.csv', "rb")
reader = csv.reader(inFile)
outFile  = open('data2.csv', "wb")
writer = csv.writer(outFile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)

# Read header
header = reader.next()

#for row in reader:
#   found = False
#   for col in row:
#       if col in [string_1, string_2, string_3] and not found:
#           writer.writerow(row)
#           found = True


#writer.writerow(header(0,2))


"""Built a list of items to sort. If row 12 contains 'LinkRunner AT' (group 1),
    one stores a triple (1, 12, row)
    When the triples are sorted later, all rows in group 0 will come first, then
    all rows in group 1, etc.
"""

stored = []
writer.writerow(row[header] for header in (0,2))

for i, row in enumerate(reader):
    g = find_group(row)
    if g >= 0:
        stored.append((g, i, row))
stored.sort()


for g, i, row in stored:
    writer.writerow(tuple(row[k] for k in (0,2))) # output col 1 & 3

#for row in reader:
 #   if any(does_match(col) for col in row):
  #      writer.writerow(row[:2]) # write only 2 first columns

# Closing Input and Output files.
inFile.close()
outFile.close()

**

2
  • 1
    Your question title says columns 0 and 2; your description says 0 and 3, and comments in your code say 1 and 3. Which do you want? Commented Feb 16, 2014 at 1:30
  • 1
    As a side note: "I'm also curious if anyone has any expereince with pushing to google docs" is both a completely separate question that has nothing to do with the one you're asking, and not an appropriate question for SO in the first place. Click help above and read about asking questions. Commented Feb 16, 2014 at 1:36

1 Answer 1

1

I think what you're looking for is this:

writer.writerow([header[0], header[2]])

You could also use either of the two more complicated mechanisms you use later in the same script:

writer.writerow(header[i] for i in (0,2))
writer.writerow(tuple(header[k] for k in (0,2)))

… but there's really no good reason to. In fact, you'd be better off changing those lines to do things the simple way. Also, you'd be better off not trying to re-use the variable header as a loop index variable… So:

for g, i, row in stored:
    writer.writerow([row[0], row[2]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.