2

Is there any way I can display leading zeros after splitting numbers into parts like first 3 digit and last 2 digit in Python 3.2? My script returns the numbers with no leading zeros...

I have a csv file that looks like this:

Name,Code
blackberry,20001
wineberry,02002
rasberry,30000
blueberry,03010

My desired output:

Name,Code,Code1,Code2
blackberry,20001,200,01
wineberry,02002,020,02
rasberry,30000,300,00   
blueberry,03010,030,10

My script:

import csv
all = []
with open('aaa.csv','r') as csvinput:
    with open('bbb.csv', 'w') as csvoutput:
        reader = csv.reader(csvinput,delimiter=',')
        writer = csv.writer(csvoutput,delimiter=",", lineterminator='\n')
        row = next(reader)
        row.append('Code1')
        row.append('Code2')
        all.append(row)
        for row in reader:
            row.append(row[1][0:2])
            row.append(row[1][-2:])
            all.append(row)
        writer.writerows(all)
        print(all)

Above script returns:

Name,Code,Code1,Code2
blackberry,20001,200,1
wineberry,02002,20,2
rasberry,30000,300,0    
blueberry,03010,30,10

2 Answers 2

6

The csv.writer does not convert strings to numerical types, so it is not causing the error you are seeing.

I expect that you are viewing your csv file in Excel, which converts strings to numbers wherever it can. Look at your output in a text editor and you should see that the leading zeros are really there.

Also, as others have pointed out your first slice should be row[1][0:3].

Sign up to request clarification or add additional context in comments.

6 Comments

+1 for the first good explanation of where the OP's output came from!
Yes I am viewing csv in Excel. So there is no way that you can keep numbers in strings in csv? As for the slice, it's my mistake..sorry for the confusion
I'm away from a windows machine right now, but I believe you can use the text import wizard on a csv file.
Here's what I found: On the Data tab, in the Get External Data group, click From Text. Then follow the steps on the import wizard. It's important that you set the columns to Text format rather than General.
If your desired output is Excel, csv is the wrong solution. Search stack overflow for 'Python', 'write', 'excel' and you should find some good solutions.
|
2

I can't see how your code could have generated this output. But correcting the spelling error, changing the for loop to

    for row in reader:
        row = [r.strip() for r in row]
        row.append(row[1][0:3])
        row.append(row[1][3:])
        all.append(row)

should work, regardless of errant whitespace which might be causing problems at the end of a line:

localhost-2:coding $ more bbb.csv 
Name,Code,Code1,Code2
blackberry,20001,200,01
wineberry,02002,020,02
raspberry,30000,300,00
blueberry,03010,030,10

PS: using all as a variable name is a bad idea. It's the name of a very handy builtin.

2 Comments

I tried the script. I also changed all to abc but still returns no leading zero..
What does "return no leading zero" mean? It's producing a file. The file either contains a zero or it doesn't, and I'm willing to take good odds that it does. Are you looking at the file in Excel, as @StevenRumbalski guessed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.