Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6775393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:54:06+00:00 2026-05-26T15:54:06+00:00

I have some large csv file, with about 200 header names (the first one

  • 0

I have some large csv file, with about 200 header names (the first one of which is empty).
I want to get some chosen columns and copy them to a new output.csv file. My problem comes grabbing the header which has no name! (empty first element in the header)

So the input.csv looks something like,

            ,header1,header2,header3,header4, ... , header200
            value0, value2, value2, value3, value4, ..., value200
            ,2,3,30,,, ... , 10
            66,2,3,30,, ... , 10

etc (all rows have the same number of elements even if empty).

After reading various questions I’ve recycled some code from
write CSV columns out in a different order in Python

to write,

import csv
from operator import itemgetter         

SelectedSignals = ['header1',  'header4'] 



fiin=open('input.csv','rb') #open to read "r" in binary mode "b"
fiout=open('output.csv','wb') #open to write "w" in binary mode "b"

reader = csv.reader(fiin, delimiter=',')
writer = csv.writer(fiout, delimiter=',')

AllSignalNames = reader.next()
name2index = dict((name, index) for index, name in enumerate(AllSignalNames))
writeindices = [name2index[name] for name in SelectedSignals]
reorderfunc = itemgetter(*writeindices) # itemgetter was imported from operator module
writer.writerow(SelectedSignals)

for row in reader:
    writer.writerow(reorderfunc(row))

this gives the desired output,
say,

            ,header1,header4
            value0, value4
            ,30
            66,30

but the problem is doing,

  SelectedSignals = [' ', 'header1',  'header4'] 

to grab the first column. which returns KeyError

I’m a python beginner, so any hints are appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:54:06+00:00Added an answer on May 26, 2026 at 3:54 pm

    In the CSV format, the first header should be a zero-length string (''), not a space (' '), which is what you use in SelectedSignals.

    You could also add a fake column name to your name2index dict, for example name2index['header0'] = 0 just after name2index = ... and then use 'header0' in SelectedSignals.

    Alternatively, you could use a default value for the dict (when it can’t find the header you want, it would use this default value): name2index.get(name, 0) instead of name2index[name] in your writeindices expression.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Requirements I have a very large CSV file to read. (about 3 GB) I
I'm looking at building a Rails application which will have some pretty large tables
I have a large set of files, some of which contain special characters in
I have a CSV file that has rows, where data for some columns only
I have some large csv files (1.5gb each) where I need to replace specific
I have a large CSV file (5.4GB) of data. It's a table with 6
I'm working with a CSV file in python, which will have ~100,000 rows when
I have a large CSV file that contains independent items that take a fair
I am trying to import a large csv file into MySQL. It's about 1.4
We have some very large data files (5 gig to 1TB) where we need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.