简体   繁体   中英

Appending a “0” to the beginning of a cell in a CSV file?

To be specific, I've got a list of about 2000 FIPS codes (example: "8001007801") in a single column of a CSV file. All of these codes should begin with a 0, but by default Excel formats the cells to remove "0" values at the beginning of a cell that contains only numbers. I know how to format this when I'm printing but not when I'm writing to a file.

I need my script to go through the first cell in each row, place a "0" at the front of it, and then copy that row to a new output CSV. Here is what I have so far:

    import csv

    inFile = open("FIPS_original.csv", "r")
    reader = csv.reader(inFile, delimiter = ";", quotechar = '"')

    outFile = open("FIPS_appended.csv", "w")
    writer = csv.writer(outFile, delimiter = ";", quotechar = '"')    

    for row in reader:
        outRow = list(row)
        outRow[0] = outRow[0].zfill(11)
        writer.writerow(outRow)

    inFile.close()
    outFile.close()

All this script does at this point is copy the same values to the output CSV, but it does not actually append the "0". Can anyone help out with this? Please let me know if you need more information, and thanks in advance.

EDIT: I should note that when I use the print function to print my values in the Python IDLE, they come out formatted correctly. I should also note that I have unchecked the Excel setting that automatically formats text cells with no alphabetical characters as numbers, and I have also tried formatting all of the cells in both spreadsheets as numbers.

ANSWER: Turns out it was indeed a formatting issue in Excel. Changing the cell type to text does nothing even if you disable the option that automatically changes text cells with only numbers to number cells. What you have to do is custom format your cell to hold however many characters you need (which doesn't solve anything if all of the numbers in the column are different characters). This link explains how to do it: https://superuser.com/questions/88870/adding-a-zero-before-values-in-an-excel-spreadsheet-column

Thanks for all the help - once enough time has passed for me to post this as an answer to my own question I will.

Try this:

import csv

fn_in="/tmp/FIPS_original.csv"
fn_out="/tmp/FIPS_appended.csv"
with open(fn_in, "r") as inFile, open(fn_out, 'w') as outFile:
    reader = csv.reader(inFile, delimiter = ";", quotechar = '"')
    writer = csv.writer(outFile, delimiter = ";", quotechar = '"')    
    header=next(reader)
    writer.writerow(header)

    for row in reader:
        row[0] = row[0].zfill(11)
        writer.writerow(row)

The behavior you are describing is probably because csv is not parsing each column in each row into a list. Try putting in a print statement to make sure that your delimiter and quote characters are correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM