I'm running in circes trying to accomplish something very simple. I have a csv file with multiple lines resembling:

0002345, 24, 5, 0.24

I need this format in my output file:
'0002345', 24, 5 0.24

The first entry is a census meshblock code and the leading zeros are relevant for comparison purposes. After extensive googling, I have found half a dozen different ways to convert 0002345 into a string, all of which work brilliantly at the prompt. However, when thrown into a for loop, the csv I get always returns the item without the 'quotes'.

Here's the fourth version that works at the prompt but not in my loop:

wf1 = open('meshoutE.txt', 'w') #output file handle
writer1 = csv.writer(wf1)
for entry in rf1:
    entry[0] = str(entry[0]).zfill(7)

I've tried many variations on the theme without success, in both 2.6 and 3.1. I cannot seem to get the quotes into the output. I am clearly missing the forest for the trees! Anyone got an ax?

5 Years
Discussion Span
Last Post by Caraka


I need this format in my output file:
'0002345', 24, 5, 0.24

Mustn't drop the commas!


May you tell, why you need single quotes, which you can do many ways, including repr function?


What does rf1 look like? Need only a few data lines.

As far as I understand csv.writer() turns all data into strings, however you can specifiy the quote character this way:
writer1 = csv.writer(wf1, quotechar="'")
I know that's hard to see, but this is a single quote inside double quotes.

You can also specify the dialect of the csv file, for instance 'excel'

Edited by vegaseat: n/a


Kia ora folks

All fair questions. I didn't articulate well in the post. I don't care if it's single or double quotes, only that my column one data is formatted as text and not a number, so that I can do a join to another database, where the meshblock field is text and includes the leading zeros. The csv.reader certainly turns everything into text which is why all my code to date has never tripped over this, as even reading from the secondary database comes in as text. However, as I am learning, whenever the csv.writer writes this to a text or csv file, it's formatted numeric, even when the input was text and even when I've converted it to string as in the above example.

Here is a sample of the rf1 input, as cut and pasted from the text editor. (That way you see the quotes where things are formatted as text) In a spreadsheet programme, all the leading zeros get truncated as the format is numeric for column one, and this format issue is preventing the join at the next step in my work:


It seems laughably simple but it has tripped me up for many an hour yesterday! If I could just get column one looking like this:

"0000100" or this:
'0000100' then Bob's my uncle!


Kia ora

Having answered vegaseat's question, I went on to test Gribouillis' suggestion, which worked like a charm. (I hadn't seen this one in all the variations google provided) For completeness, I commented out my str.zfill and added the suggestion, adding a missing pair of brackets to his/her opening variable:

wf1 = open('meshoutE.txt', 'w') #output file handle
writer1 = csv.writer(wf1)
for entry in rf1:
    entry[0] = "'{e:0>7}'".format(e=entry[0])
    #entry[0] = str(entry[0]).zfill(7)

The input from rf1 (pasted above) came out like this:

My aging database was happy and the join went well while I had a fine breakfast. Gold star!

Many thanks! I have rarely posted since 2007 because I usually find the answer first and am not often competent to answer other's queries. It's a great resource!

Votes + Comments
thanks for letting us know
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.