-2
$ cat customer.csv
customerno, firstname, lastname, sales
23242, john, doe, 2345.00
23253, jane, doe, 1234.00
23221, greg, johnson, 2345.00
23210, howard, gardner, 2345.00

以下代码按预期工作,它将反转列顺序。

import csv
with open('customer2.csv', 'wb') as output:
    input = csv.reader(open('customer.csv', 'rb'))
    output = csv.writer(output, dialect=input.dialect)
    for line in input:
        line.reverse()
        output.writerow(line)

$ cat customer2.csv
 sales, lastname, firstname,customerno
 2345.00, doe, john,23242
 1234.00, doe, jane,23253
 2345.00, johnson, greg,23221
 2345.00, gardner, howard,23210

但我真正需要的不是 4,3,2,1 而是 4,2,3,1 序列。预期输出:

$ cat newfile.csv
 sales, lastname, firstname, lastname, customerno
 2345.00, john, doe, 23242
 1234.00, jane, doe, 23253
 2345.00, greg, johnson, 23221
 2345.00, howard, gardner, 23210
4

1 回答 1

4
(customerno, firstname, lastname, sales) = line
outLine = (sales, firstname, lastname, customerno)
output.writerow(outLine)
于 2012-10-15T10:38:36.360 回答