简体   繁体   English

你如何使用python在csv文件中保持前导零?

[英]how do you keep leading zeros in a csv file using python?

Lets say my csv file looks something like this:假设我的 csv 文件如下所示:

acc_num,pincode
023213821,23120
002312727,03131
231238782,29389
008712372,00127
023827812,23371

when I open this file in excel , it removes the leading zeros , but here I need to keep them .当我在 excel 中打开这个文件时,它会删除前导零,但在这里我需要保留它们。 This is how it looks when i open it in excel这是我在excel中打开时的样子

acc_num,pincode
23213821,23120
2312727,3131
231238782,29389
8712372,127
23827812,23371

I tried converting this into a string but it still shows it without 0 (but its a string now) I tried using the astype() function from pandas but there's no point in using it我尝试将其转换为字符串,但它仍然显示它没有 0(但它现在是一个字符串)我尝试使用 pandas 的 astype() 函数,但没有必要使用它

Any help would be appreciated任何帮助,将不胜感激

您是否将 excel 文档中的单元格格式化为“文本”,以便当您在 excel 中打开它时显示前导零,然后当您将其引入 Python 时,确保将其引入并存储为字符串,如python3 不允许整数前导零。

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

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