Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改列顺序_Python_Csv - Fatal编程技术网

Python 更改列顺序

Python 更改列顺序,python,csv,Python,Csv,下面的代码按预期工作,它将改变列顺序 $ 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 但我真正需要的不是4,3,2,1而是4,2,3,1序列。预期产出: import csv with open('cu

下面的代码按预期工作,它将改变列顺序

$ 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
但我真正需要的不是4,3,2,1而是4,2,3,1序列。预期产出:

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
获取错误#ValueError:需要超过0个值才能解包
$ 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
(customerno, firstname, lastname, sales) = line
outLine = (sales, firstname, lastname, customerno)
output.writerow(outLine)