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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:26:56+00:00 2026-05-26T21:26:56+00:00

I am trying to create a csv file using python that is truly Excel-compatible

  • 0

I am trying to create a csv file using python that is truly Excel-compatible (I am using Excel 2007, if that makes any difference).

Here is the sort of thing I’m trying to do

import csv

data = [ ['9-1', '9-2', '9-3'] ]

fd = open('test.csv', 'wb')
try:
    writer = csv.writer(fd, dialect='excel', quotechar='"', quoting=csv.QUOTE_ALL)
    writer.writerows(data)

finally:
    fd.close()

This creates a csv file that contains:

"9-1","9-2","9-3"

When I load it in Excel, I get the columns:

09-Jan  09-Feb  09-Mar

[Grrr, thanks Excel. Seriously, what is the point of quotes?]

Now, I have read elsewhere that in order to get Excel to treat strings as literal they must be prefixed by an equals sign ="Like This". So what I really want to produce is:

="9-1",="9-2",="9-3"

Is there any way to do this with the csv module in python? I’m guessing it will involve creating a custom dialect, but I can’t see any variable that would give me the desired result.

  • 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-26T21:26:56+00:00Added an answer on May 26, 2026 at 9:26 pm

    Ok, thanks for all your help, I’ve come up with a rough solution, so I thought I’d share it here.

    It’s not the prettiest of solutions but it works for my purposes. A warning though: It will break if you have any quotes in your strings!

    import csv
    import re
    
    data = [ ['9-1', '9-2', '9-3'] ]
    
    
    def quoteCsvData(data):
        for x, row in enumerate(data):
            for y, item in enumerate(row):
                 if data[x][y] != '':
                     data[x][y] = r'="' + data[x][y] + r'"'
        return data
    
    def unquoteCsvData(data):
        for x, row in enumerate(data):
            for y, item in enumerate(row):
                if data[x][y] != '':
                    m = re.match(r'="([^"]*)"',data[x][y])
                    if m:
                        data[x][y] =  m.group(1)
        return data
    
    
    fd = open('test2.csv', 'wb')
    data = quoteCsvData(data)
    try:
        writer = csv.writer(fd, delimiter=',',
                                quotechar='|', 
                                quoting=csv.QUOTE_NONE, 
                                escapechar='\\')
        writer.writerows(data)
    
    finally:
        fd.close()
    
    fd = open('test2.csv', 'rb')
    try:
        reader = csv.reader(fd, delimiter=',', 
                                quotechar='|', 
                                quoting=csv.QUOTE_NONE, 
                                escapechar='\\')
        data = []
        for row in reader:
            data.append(row)
    
    finally:
        fd.close()
    
    data = unquoteCsvData(data)
    
    print data
    

    Update: removed spamming of empty cells with lots of =""

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

Sidebar

Related Questions

I'm trying to create a simple Silverlight application that involves parsing a CSV file
I am trying to write to a .tsv file using python's CSV module, this
I am trying to create a CSV file using the C# code below: string
I am trying to create a csv file with UTF8 with BOM using c++.
I'm trying to create a csv file that contains the contents of a list
I'm trying to create a CSV file using php. How can I print non
Ok so I am trying create a login script, here I am using PHP5
I am trying to create a csv file of some data. I have wrote
I'm trying to create an sqlite database by importing a csv file with urls.
I am trying to create a new CSV file then write to it, the

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.