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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:11:03+00:00 2026-05-31T12:11:03+00:00

f = open(file1.csv, r) g = open(file2.csv, w) a = csv.reader(f, delimiter=;, skipinitialspace=True) b

  • 0
f = open("file1.csv", "r")
g = open("file2.csv", "w")

a = csv.reader(f, delimiter=";", skipinitialspace=True)
b = csv.writer(g, delimiter=";")

for line in a:
    b.writerow(line)

In the above code, I try to load file1.csv using the csv module in Python2.7, and then write it in file2.csv using a csv.writer.

My issue comes from existing whitespaces (a single space character) after the delimiter in the input file. I need to remove them in order to do some data manipulation later on, so I used the skipinitialspace=True argument for the reader. However, I cannot get the writer to print the space char after the delimiter, and therefore disturbing any subsequent diffing of the two files.

I tried to use the Sniffer class to auto-generate a Dialect but I guess my input files (coming from a large complex legacy system, with dozens of fields and poor quoting and escaping) are proving to be too complex for this.

In more simple terms I’m looking for the answers to the following questions:

  • How can I insert a space character after each delimiter in the writer?
  • Incidently, what are the reasons to prohibit the use of multi-character strings as delimiters? delimiter="; " would’ve solved my problem.
  • 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-31T12:11:04+00:00Added an answer on May 31, 2026 at 12:11 pm

    You can wrap your file objects in proxies that add the whitespace:

    >>> class DelimitedFile(file):
    ...     def write(self, value):
    ...             super(DelimitedFile, self).write(value.replace(";", "; "))
    ... 
    >>> f = DelimitedFile("foo", "w")
    >>> f.write("hello;world")
    >>> f.close()
    >>> open("foo").read()
    'hello; world'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to read headers for a csv file with: reader = csv.DictReader(open(PATH_FILE),skipinitialspace=True) headers =
I have the following code: ifstream initFile; initFile.open(D:\\InitTLM.csv); if(initFile.is_open()) { // Process file }
I tried this code to open a file in Python: f = open("/Desktop/temp/myfile.txt","file1") It
In a basic I had the next process. import csv reader = csv.reader(open('huge_file.csv', 'rb'))
I have CSV file and Macro in VBA. I want to open CSV file
I've been using File.ReadAllText() to open a CSV file, but every time I forget
I want to be able to use Python to open a .csv file like
I used Python to generate a CSV file. But when I open it in
var doc = w.document; doc.open('application/CSV','replace'); doc.charset = utf-8; doc.write(all,hello); doc.close(); if(doc.execCommand(SaveAs,null,file.csv)) { window.alert(saved );
I'm using the CSV module to read a tab delimited file. Code below: z

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.