简体   繁体   English

在CSV文件的单元格开头添加“ 0”?

[英]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. 具体来说,我在CSV文件的单列中列出了大约2000个FIPS代码(例如:“ 8001007801”)。 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. 所有这些代码都应以0开头,但是默认情况下,Excel格式化单元格以删除仅包含数字的单元格开头的“ 0”值。 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. 我需要我的脚本浏览每行的第一个单元格,在其前面放置一个“ 0”,然后将该行复制到新的输出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". 此脚本在此刻所做的所有操作都是将相同的值复制到输出CSV,但实际上并没有附加“ 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. 编辑:我应该注意,当我使用打印功能在Python IDLE中打印我的值时,它们的格式正确。 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. 我还应该注意,我没有选中Excel设置,该设置会自动将没有字母字符的文本单元格设置为数字格式,并且还尝试将两个电子表格中的所有单元格设置为数字格式。

ANSWER: Turns out it was indeed a formatting issue in Excel. 解答:事实证明,这确实是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 该链接说明了如何执行此操作: 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. 您描述的行为可能是因为csv没有将每一行中的每一列解析为一个列表。 Try putting in a print statement to make sure that your delimiter and quote characters are correct. 尝试输入打印语句,以确保分隔符和引号字符正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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