-1

How can I re-order the columns of a CSV file using Python? These are the first rows of a CSV file I need to change:

03;30269714;Ramiro Alberto;Nederz;active;pgc_gral
03;36185520;Andrea;Espare;active;pgc_gral
03;24884344;Maria Roxana;Nietto;active;pgc_gral
03;27461021;Veronica Andrea;Lantier;active;pgc_gral
71;24489743;Diego;Moneta;active;pgc_gral

This is the desired output:

30269714;pgc_gral; Ramiro Alberto;Nederz
36185520;pgc_gral; Andrea;Espare
24884344;pgc_gral;Maria Roxana;Nietto
27461021;pgc_gral;Veronica Andrea;Lantier
24489743;pgc_gral;Diego;Moneta

Column 2 is now column 1, column 6 is column 2, columns 3 and 4 should stay the same and column 5 should be discarded.

08Dc91wk
  • 3,970
  • 6
  • 28
  • 64
MV_81
  • 93
  • 8
  • 3
    Did you try to solve it already? post some code that we can review and help guide you – Incognos Jul 27 '15 at 20:29
  • Take a look at http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html, csv isn't sophisticated enough to do translations – FirebladeDan Jul 27 '15 at 20:30

1 Answers1

1

Try this:

import csv

with open('yourfile.csv') as f:
    reader = csv.reader(f, delimiter=';')
    for row in reader:
        print(";".join([row[1], row[5], row[2], row[3]]))
Ewan
  • 12,912
  • 3
  • 45
  • 55