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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:40:25+00:00 2026-05-30T05:40:25+00:00

I want to combine combine fields from an input .csv file for output to

  • 0

I want to combine combine fields from an input .csv file for output to a .csv file, and some contain commas. Here is my code, simplified

outfile = open('output.csv', 'w')

#these values are made up for this example; normally they would be read from
#a csv and passed to the following 'combine()' function

a = "John"  
b = ",Jr."  

def combine(a, b):  
    if a == "":  
        pass  #don't write anything if the field is empty
    else:  
        outfile.write(a)  
    if b =="":  
        pass  
    else:  
        outfile.write(b)  

If b starts with a comma, how do I make the output “John, Jr.” ? I have tried using the csv.writer writerow() but it puts a comma delimiter between each character. I have tried defining an escapechar but it just outputs “John \” , “Jr.” Suggestions?

  • 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-30T05:40:26+00:00Added an answer on May 30, 2026 at 5:40 am

    If you would like to know details about CSV, there is a spec: https://www.rfc-editor.org/rfc/rfc4180

    In general it states the following
    "Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes."

    "If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote."

    Implementations like Excel always put all field values into double quotes.

    If you open a file for read or write you can specify the type of quoting directly

    mcvs = csv.writer(open('file.csv', 'wb'), quoting=csv.QUOTE_ALL)
    

    will always add quotes to the around the field value.

    For all possible values look at the python documentation

    http://docs.python.org/library/csv.html#module-csv

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

Sidebar

Related Questions

In a table I want to combine some of the columns, but not in
* revised, but still not solved...* I want to combine data from two files
how to combine 2 different tables where they contain the same fields but different
I have multiple columns in an Access DB, I want to combine the fields
I want to combine the following queries into a single query: SELECT val FROM
I want to combine values from multiple lines with different lengths using awk into
I want to combine these: keys = ['name', 'age', 'food'] values = ['Monty', 42,
I want to combine three tables - date, lead and click - in a
I want to combine two array's, excluding duplicates. I am using a custom class:
I want to combine these two queries into one: mysql_query(INSERT INTO categories (name, parent)

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.