Skip to main content
rollback to the original question
Source Link
LancelotHolmes
  • 709
  • 1
  • 12
  • 31

The issue is common, when I import a csv file and process it and finally output it, the order of column in the output csv file may be different from the original one,for instance:

dct={}
dct['a']=[1,2,3,4]
dct['b']=[5,6,7,8]
dct['c']=[9,10,11,12]

header = dct.keys()
rows=pd.DataFrame(dct).to_dict('records')

with open('outTest.csv', 'wb') as f:
    f.write(','.join(header))
    f.write('\n')
    for data in rows:
        f.write(",".join(str(data[h]) for h in header))
        f.write('\n')

the original csv file is like:

a,c,b
1,9,5
2,10,6
3,11,7
4,12,8

while I'd like to fixed the order of the column like the output:

a,b,c
1,5,9
2,6,10
3,7,11
4,8,12

and the answers I found are mostly related to pandas, I wonder if we can solve this in another way.


Well,it's my fault that I have make my actual issues changed, to start with,thanks for the help of @Jean-François Fabre and @Fejs, their recommendation on the utilize of OrderedDict() works well for the previous problem, but actually I'd like to read from a csv file and do sth with it and finally output it with a set-by-hand column order,for example I have a csv file like:

a,b,c
1,5,9
2,6,10
3,7,11
4,8,12

And after read and several provess,I want to output it as a new csv file with an preserving or even elastic set-by-hand order like:

a,b,c
1,5,9
2,6,10
......

or

c,b,a
9,5,1
10,6,2
......

and my code is like:

def csv_rewrite(in_file,out_file):
    with open(in_file,'rb') as f:
        reader=csv.DictReader(f)
        rows=[row for row in reader]
    header=rows[0].keys()
    with open(out_file,'wb') as f:
        f.write(','.join(header))
        f.write('\n')
        for data in rows:
            f.write(",".join(data[h] for h in header))
            f.write('\n')

Sorry for causing such trouble,I meant to make my explanation simpler but it seemed to be puzzled.

Any help is appreciated, thank you.

The issue is common, when I import a csv file and process it and finally output it, the order of column in the output csv file may be different from the original one,for instance:

dct={}
dct['a']=[1,2,3,4]
dct['b']=[5,6,7,8]
dct['c']=[9,10,11,12]

header = dct.keys()
rows=pd.DataFrame(dct).to_dict('records')

with open('outTest.csv', 'wb') as f:
    f.write(','.join(header))
    f.write('\n')
    for data in rows:
        f.write(",".join(str(data[h]) for h in header))
        f.write('\n')

the original csv file is like:

a,c,b
1,9,5
2,10,6
3,11,7
4,12,8

while I'd like to fixed the order of the column like the output:

a,b,c
1,5,9
2,6,10
3,7,11
4,8,12

and the answers I found are mostly related to pandas, I wonder if we can solve this in another way.


Well,it's my fault that I have make my actual issues changed, to start with,thanks for the help of @Jean-François Fabre and @Fejs, their recommendation on the utilize of OrderedDict() works well for the previous problem, but actually I'd like to read from a csv file and do sth with it and finally output it with a set-by-hand column order,for example I have a csv file like:

a,b,c
1,5,9
2,6,10
3,7,11
4,8,12

And after read and several provess,I want to output it as a new csv file with an preserving or even elastic set-by-hand order like:

a,b,c
1,5,9
2,6,10
......

or

c,b,a
9,5,1
10,6,2
......

and my code is like:

def csv_rewrite(in_file,out_file):
    with open(in_file,'rb') as f:
        reader=csv.DictReader(f)
        rows=[row for row in reader]
    header=rows[0].keys()
    with open(out_file,'wb') as f:
        f.write(','.join(header))
        f.write('\n')
        for data in rows:
            f.write(",".join(data[h] for h in header))
            f.write('\n')

Sorry for causing such trouble,I meant to make my explanation simpler but it seemed to be puzzled.

Any help is appreciated, thank you.

The issue is common, when I import a csv file and process it and finally output it, the order of column in the output csv file may be different from the original one,for instance:

dct={}
dct['a']=[1,2,3,4]
dct['b']=[5,6,7,8]
dct['c']=[9,10,11,12]

header = dct.keys()
rows=pd.DataFrame(dct).to_dict('records')

with open('outTest.csv', 'wb') as f:
    f.write(','.join(header))
    f.write('\n')
    for data in rows:
        f.write(",".join(str(data[h]) for h in header))
        f.write('\n')

the original csv file is like:

a,c,b
1,9,5
2,10,6
3,11,7
4,12,8

while I'd like to fixed the order of the column like the output:

a,b,c
1,5,9
2,6,10
3,7,11
4,8,12

and the answers I found are mostly related to pandas, I wonder if we can solve this in another way.

Any help is appreciated, thank you.

read data from csv file and output it with specific order after processing
Source Link
LancelotHolmes
  • 709
  • 1
  • 12
  • 31

The issue is common, when I import a csv file and process it and finally output it, the order of column in the output csv file may be different from the original one,for instance:

dct={}
dct['a']=[1,2,3,4]
dct['b']=[5,6,7,8]
dct['c']=[9,10,11,12]

header = dct.keys()
rows=pd.DataFrame(dct).to_dict('records')

with open('outTest.csv', 'wb') as f:
    f.write(','.join(header))
    f.write('\n')
    for data in rows:
        f.write(",".join(str(data[h]) for h in header))
        f.write('\n')

the original csv file is like:

a,c,b
1,9,5
2,10,6
3,11,7
4,12,8

while I'd like to fixed the order of the column like the output:

a,b,c
1,5,9
2,6,10
3,7,11
4,8,12

and the answers I found are mostly related to pandas, I wonder if we can solve this in another way. Any


Well,it's my fault that I have make my actual issues changed, to start with,thanks for the help of @Jean-François Fabre and @Fejs, their recommendation on the utilize of OrderedDict() works well for the previous problem, but actually I'd like to read from a csv file and do sth with it and finally output it with a set-by-hand column order,for example I have a csv file like:

a,b,c
1,5,9
2,6,10
3,7,11
4,8,12

And after read and several provess,I want to output it as a new csv file with an preserving or even elastic set-by-hand order like:

a,b,c
1,5,9
2,6,10
......

or

c,b,a
9,5,1
10,6,2
......

and my code is like:

def csv_rewrite(in_file,out_file):
    with open(in_file,'rb') as f:
        reader=csv.DictReader(f)
        rows=[row for row in reader]
    header=rows[0].keys()
    with open(out_file,'wb') as f:
        f.write(','.join(header))
        f.write('\n')
        for data in rows:
            f.write(",".join(data[h] for h in header))
            f.write('\n')

Sorry for causing such trouble,I meant to make my explanation simpler but it seemed to be puzzled.

Any help is appreciated, thank you.

The issue is common, when I import a csv file and process it and finally output it, the order of column in the output csv file may be different from the original one,for instance:

dct={}
dct['a']=[1,2,3,4]
dct['b']=[5,6,7,8]
dct['c']=[9,10,11,12]

header = dct.keys()
rows=pd.DataFrame(dct).to_dict('records')

with open('outTest.csv', 'wb') as f:
    f.write(','.join(header))
    f.write('\n')
    for data in rows:
        f.write(",".join(str(data[h]) for h in header))
        f.write('\n')

the original csv file is like:

a,c,b
1,9,5
2,10,6
3,11,7
4,12,8

while I'd like to fixed the order of the column like the output:

a,b,c
1,5,9
2,6,10
3,7,11
4,8,12

and the answers I found are mostly related to pandas, I wonder if we can solve this in another way. Any help is appreciated, thank you.

The issue is common, when I import a csv file and process it and finally output it, the order of column in the output csv file may be different from the original one,for instance:

dct={}
dct['a']=[1,2,3,4]
dct['b']=[5,6,7,8]
dct['c']=[9,10,11,12]

header = dct.keys()
rows=pd.DataFrame(dct).to_dict('records')

with open('outTest.csv', 'wb') as f:
    f.write(','.join(header))
    f.write('\n')
    for data in rows:
        f.write(",".join(str(data[h]) for h in header))
        f.write('\n')

the original csv file is like:

a,c,b
1,9,5
2,10,6
3,11,7
4,12,8

while I'd like to fixed the order of the column like the output:

a,b,c
1,5,9
2,6,10
3,7,11
4,8,12

and the answers I found are mostly related to pandas, I wonder if we can solve this in another way.


Well,it's my fault that I have make my actual issues changed, to start with,thanks for the help of @Jean-François Fabre and @Fejs, their recommendation on the utilize of OrderedDict() works well for the previous problem, but actually I'd like to read from a csv file and do sth with it and finally output it with a set-by-hand column order,for example I have a csv file like:

a,b,c
1,5,9
2,6,10
3,7,11
4,8,12

And after read and several provess,I want to output it as a new csv file with an preserving or even elastic set-by-hand order like:

a,b,c
1,5,9
2,6,10
......

or

c,b,a
9,5,1
10,6,2
......

and my code is like:

def csv_rewrite(in_file,out_file):
    with open(in_file,'rb') as f:
        reader=csv.DictReader(f)
        rows=[row for row in reader]
    header=rows[0].keys()
    with open(out_file,'wb') as f:
        f.write(','.join(header))
        f.write('\n')
        for data in rows:
            f.write(",".join(data[h] for h in header))
            f.write('\n')

Sorry for causing such trouble,I meant to make my explanation simpler but it seemed to be puzzled.

Any help is appreciated, thank you.

Source Link
LancelotHolmes
  • 709
  • 1
  • 12
  • 31

python preserving output csv file column order

The issue is common, when I import a csv file and process it and finally output it, the order of column in the output csv file may be different from the original one,for instance:

dct={}
dct['a']=[1,2,3,4]
dct['b']=[5,6,7,8]
dct['c']=[9,10,11,12]

header = dct.keys()
rows=pd.DataFrame(dct).to_dict('records')

with open('outTest.csv', 'wb') as f:
    f.write(','.join(header))
    f.write('\n')
    for data in rows:
        f.write(",".join(str(data[h]) for h in header))
        f.write('\n')

the original csv file is like:

a,c,b
1,9,5
2,10,6
3,11,7
4,12,8

while I'd like to fixed the order of the column like the output:

a,b,c
1,5,9
2,6,10
3,7,11
4,8,12

and the answers I found are mostly related to pandas, I wonder if we can solve this in another way. Any help is appreciated, thank you.