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 9147965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:08:13+00:00 2026-06-17T11:08:13+00:00

I have not used the csv module in python before, but it seems like

  • 0

I have not used the csv module in python before, but it seems like a great tool to use for my needs.

The problem I am facing is that my csv-file looks a bit strange. (it was hard to notice at first)

See the example below for what I mean:

A|B|C|D|x|F|G|x|H
1||2||3|4||5|6
1||2||3|4||5|6
1||2||3|4||5|6
1||2||3|4||5|6

There are some indexes (such as column-x) above which should be read as Dx and Gx. The D-column and G-column are then empty columns.

So what I need is that when csv is processing the header it should create the headers like Dx and Gx and neglect the D and G columns and take the value from the x-column instead.

(I tried to read the file as is with the following code first and post process it, but it is not possible since the column name x is always the same, and csv.DictReader seems to discard repetitive column-names)

with open('myCSV.log') as csvFile:
    reader = csv.DictReader(csvFile, dialect='pipes')
    for row in reader:
        print row

Can this be achieved with a dialect, or some other fancy way of configuring the csv-module or do I have to parse this myself?

  • 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-06-17T11:08:15+00:00Added an answer on June 17, 2026 at 11:08 am

    Use pandas:

    import pandas as pd
    print pd.read_csv('test.csv',sep='|')
    

    out:

       A   B  C   D  x  F   G  x.1  H
    0  1 NaN  2 NaN  3  4 NaN    5  6
    1  1 NaN  2 NaN  3  4 NaN    5  6
    2  1 NaN  2 NaN  3  4 NaN    5  6
    3  1 NaN  2 NaN  3  4 NaN    5  6
    

    and if you need you can convert it to a dict using to_dict() method:

    {'A': {0: 1, 1: 1, 2: 1, 3: 1},
     'B': {0: nan, 1: nan, 2: nan, 3: nan},
     'C': {0: 2, 1: 2, 2: 2, 3: 2},
     'D': {0: nan, 1: nan, 2: nan, 3: nan},
     'F': {0: 4, 1: 4, 2: 4, 3: 4},
     'G': {0: nan, 1: nan, 2: nan, 3: nan},
     'H': {0: 6, 1: 6, 2: 6, 3: 6},
     'x': {0: 3, 1: 3, 2: 3, 3: 3},
     'x.1': {0: 5, 1: 5, 2: 5, 3: 5}}
    

    EDIT:
    If you need certain names for columns you can do this:

    import pandas as pd
    df = pd.read_csv('test.csv',sep='|')
    df.columns = [df.columns[index-1]+'x' if 'x' in name 
                  else name for index,name in enumerate(df.columns)]
    print df
    
       A   B  C   D  Dx  F   G  Gx  H
    0  1 NaN  2 NaN   3  4 NaN   5  6
    1  1 NaN  2 NaN   3  4 NaN   5  6
    2  1 NaN  2 NaN   3  4 NaN   5  6
    3  1 NaN  2 NaN   3  4 NaN   5  6
    

    If you want to lose the empty cols:

    print df.dropna(axis=1,how='all')
    
       A  C  Dx  F  Gx  H
    0  1  2   3  4   5  6
    1  1  2   3  4   5  6
    2  1  2   3  4   5  6
    3  1  2   3  4   5  6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have not used valgrind before, but I need to use it to check
I have not used ASP.NET Membership and Role Providers before but looking for a
I have not used many lambda expressions before and I ran into a case
Disclaimer: I have not used RoR, and I have not generated tests. But, I
I have a UTF-16 CSV file which I have to read. Python csv module
I have not used jquery much yet and not very familiar with it. Trying
I have not used Twitter enough to become familiar with its terminology or the
I have integrate draw line in my application i have not used OpenGL or
I'm working on a security project in Javascript (something I honestly have not used),
How to make the forms non scrollable in sencha touch? I have not used

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.