0

i'm struggling a bit here. I have the following code (with the help of some other great python experts here):

skip = [11, 15, 30, 31, 32, 34, 41, 45, 55, 58, 59, 62, 64, 65, 66, 67, 68, 69,
            71, 72, 73, 74, 75, 76, 77, 78, 79, 80]
    #skip += range(64, 70)
    #skip += range(71, 81)

    with open(outname, 'r') as r, open('import2SFDC.csv', 'w') as w:
        reader = csv.reader(r)
        writer = csv.writer(w, delimiter=',', quoting=csv.QUOTE_ALL)
        header = next(reader)    # "next" is the first row in this case, i.e. the header row
        header.append('SFDCID')  # Add the new column
        writer.writerow(header)  # write out the new header
        for row in reader:
            new_row = [v for k, v in enumerate(row) if k not in skip]
            new_row.append(args.SFDCID)
            writer.writerow(new_row)

For whatever reason skipping the columns defined in the skip variable doesn't work. I'm sure it's an obvious thing, but I'm looking into the code and trying to fix it for hours now, I just can't get it to work.

any help is highly appreciated.

thanks

J Richard Snape
  • 20,116
  • 5
  • 51
  • 79
f0rd42
  • 1,429
  • 4
  • 19
  • 30
  • 1
    what does your input look like? You should also make skip a set – Padraic Cunningham Feb 24 '15 at 15:17
  • 2
    Are you sure that the skip isn't working? You're not using the `skip` in your header row, so all headers will be written. Is this what you're observing? Are the following rows the same length as the header row? – J Richard Snape Feb 24 '15 at 15:19
  • 1
    If the above is the problem - the fix is a 1 liner : `header = [v for k, v in enumerate(header) if k not in skip]` just after you do `header = next(reader)` – J Richard Snape Feb 24 '15 at 15:20
  • @JRichardSnape I said it's probably obvious .... thanks a lot, that did the trick. now I just need to fix that stupid excel date thing (number of days since 01.01.1900) – f0rd42 Feb 24 '15 at 15:29
  • @JRichardSnape I have seen this thread (and numerous others) but still struggling to get this into my code as the position of the date fields in the original xls file can vary. see here: http://stackoverflow.com/questions/28609367/read-xls-convert-all-dates-into-proper-format-write-to-cvs – f0rd42 Feb 24 '15 at 15:39
  • 1
    Put something on that Q also. – J Richard Snape Feb 24 '15 at 15:59

1 Answers1

2

I think the skip functionality is working, but only on the data rows - you're not using the skip in your header row, so all headers will be written.

The fix is a 1 liner to ensure that the corresponding headers are skipped too:

...
header = next(reader)
header = [v for k, v in enumerate(header) if k not in skip]
...
J Richard Snape
  • 20,116
  • 5
  • 51
  • 79