如何从原始CSV文件中写入第0列和第2列标题?

时间:2014-02-16 01:26:28

标签: python csv header

我想知道是否有人可以告诉我如何将原始CSV文件中的第0列和第3列的标头写入新的CSV文件?我也很好奇是否有人有任何推动谷歌文档的经历?

**

#!/usr/bin/python

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


email = "myemail@gmail.com" 
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()

**

1 个答案:

答案 0 :(得分:1)

我认为您正在寻找的是:

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

您也可以使用稍后在同一脚本中使用的两种更复杂的机制之一:

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

...但是没有充分的理由。事实上,你最好以简单的方式改变这些行来做事。另外,最好不要尝试将变量header重新用作循环索引变量......所以:

for g, i, row in stored:
    writer.writerow([row[0], row[2]])
相关问题