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

  • Home
  • SEARCH
  • 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 7442287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:09:08+00:00 2026-05-29T11:09:08+00:00

I would like to use python read and write files of the following format:

  • 0

I would like to use python read and write files of the following format:

#h -F, field1 field2 field3
a,b,c
d,e,f
# some comments
g,h,i

This file closely resembles a typical CSV, except for the following:

  1. The header line starts with #h
  2. The second element of the header line is a tag to denote the delimiter
  3. The remaining elements of the header are field names (always separated by a single space)
  4. Comment lines always start with # and can be scattered throughout the file

Is there any way I can use csv.DictReader() and csv.DictWriter() to read and write these files?

  • 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-29T11:09:08+00:00Added an answer on May 29, 2026 at 11:09 am

    You can parse the first line separately to find the delimiter and fieldnames:

        firstline = next(f).split()
        delimiter = firstline[1][-1]
        fields = firstline[2:]
    

    Note that csv.DictReader can take any iterable as its first argument. So to skip the comments, you can wrap f in an iterator (skip_comments) which yields only non-comment lines:

    import csv
    def skip_comments(iterable):
        for line in iterable:
            if not line.startswith('#'):
                yield line
    
    with open('data.csv','rb') as f:
        firstline = next(f).split()
        delimiter = firstline[1][-1]
        fields = firstline[2:]
        for line in csv.DictReader(skip_comments(f),
                                   delimiter = delimiter, fieldnames = fields):
            print line
    

    On the data you posted this yields

    {'field2': 'b', 'field3': 'c', 'field1': 'a'}
    {'field2': 'e', 'field3': 'f', 'field1': 'd'}
    {'field2': 'h', 'field3': 'i', 'field1': 'g'}
    

    To write a file in this format, you could use a header helper function:

    def header(delimiter,fields):
        return '#h -F{d} {f}\n'.format(d = delimiter, f=' '.join(fields))
    
    with open('data.csv', 'rb') as f:
        with open('output.csv', 'wb') as g:
            firstline = next(f).split()
            delimiter = firstline[1][-1]
            fields = firstline[2:]
            writer = csv.DictWriter(g, delimiter = delimiter, fieldnames = fields)
            g.write(header(delimiter,fields))
            for row in csv.DictReader(skip_comments(f),
                                       delimiter = delimiter, fieldnames = fields):
                writer.writerow(row)
                g.write('# comment\n')
    

    Note that you can write to output.csv using g.write (for header or comment lines) or writer.writerow (for csv).

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

Sidebar

Related Questions

I would like to use some code written in python (it uses built in
I would like to use Python to script an application that advertises itself as
I would like to use Python's JSON module. It was only introduced in Python
I would like to use python to make system calls to programs and time
I would like to use my laptop as a web development (PHP, Python, etc.)
I use tabs for indentation in my python programs, but I would like to
I would like to write a QML Extension using only Python. I know how
I would like to know how to use python to make calls to a
I would like to write a small commandline scanner using ClamAV and Python. I
For a small python script I would like to use a temporary file with

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.