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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:22:23+00:00 2026-05-31T18:22:23+00:00

I am reading in a csv file in python, changing a few values, and

  • 0

I am reading in a csv file in python, changing a few values, and writing it back out. I read in the file using:

bomReader = csv.reader( open( args.filename, 'rb' ), delimiter=',' )
for row in bomReader:
    #do stuff

One of the fields contains values such as 0406, where the leading zero is important. After reading the csv file, if I print this field using say print row[2], it displays correctly with the leading 0.

When I write this back in to my new csv file however, the output has no leading zero. I assume this is because it is considered to be an int and the leading zero is trimmed. As I can’t use format specifiers in the csv.writer writeRow() function, what is the correct way to prevent this behaviour?

Code:

bomReader = csv.reader( open( args.filename, 'rb' ), delimiter=',' )
bomWriter = csv.writer(open(outfile, 'wb'), delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)

for row in bomReader:
try:
    qty = int(row[3])
    if (qty > 0):
        cs = row[4].split(';')
        for each in cs:
            bomWriter.writerow([row[0],row[1], row[2],'1',each.strip(),row[5],row[6],row[7],row[8],row[9]])
    elif (qty == 0):
        print "Deleted line"
    else:
        bomWriter.writerow(row)
except ValueError:
        bomWriter.writerow(row)

Sample row from the input csv file:

1007,C,0406,2,C456;C219,ANY,,,,,,

Rows in output csv file:

1007,C,406,1,C456,ANY,,,,
1007,C,406,1,C219,ANY,,,,
  • 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-31T18:22:25+00:00Added an answer on May 31, 2026 at 6:22 pm

    The codes you supplied are working correctly as you want under Python 2.6.7 and Python 2.7.2, csv.__version=='1.0'. Output:

    1007,C,0406,1,C456,ANY,,,,
    1007,C,0406,1,C219,ANY,,,,
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Python's csv module to do some reading and writing of csv files.
I need to read csv file twice. but after first reading: using (var csvReader
I am reading .csv file through PHP using fgetcsv(). Now I am able to
I am using the following method for reading Csv file content: /// <summary> ///
I have .zip file which contain csv data. I am reading .zip file using
i am reading a csv file into a list of a list in python.
I am having trouble reading a csv file, delimited by tabs, in python. I
im reading a csv file and then writing a new one: import csv with
I'm reading a CSV file with Fast CSV Reader (on codeproject). When I print
I have a weird issue reading a .csv file using c#. Here are 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.