简体   繁体   English

导出到CSV,在Excel打开时保持前导零

[英]Export to CSV, keeping leading zeros when opened in Excel

I have a series of massive data files that range in size from 800k to 1.4M rows, and one variable in particular has a set length of 12 characters (numeric data but with leading zeros where other the number of non-zero digits is fewer than 12).我有一系列大小从 800k 到 1.4M 行不等的海量数据文件,特别是一个变量的设置长度为 12 个字符(数字数据,但前导零,其他非零数字的数量少于12). The column should look like this:该列应如下所示:

col
000000000003
000000000102
000000246691
000000000042
102851000324

etc.等等

I need to export these files for a client to a CSV file, using R. The final data NEEDS to retain the 12 character structure, but when I open the CSV files in excel, the zeros disappear.我需要使用 R 将客户端的这些文件导出到 CSV 文件。最终数据需要保留 12 个字符的结构,但是当我在 excel 中打开 CSV 文件时,零消失了。 This happens even after converting the entire data frame to character.即使在将整个数据框转换为字符后也会发生这种情况。 The code I am using to do this is as follows.我用来执行此操作的代码如下。

df1 %>%
mutate(across(everything(), as.character))
##### I did this for all data frames #####


export(df1, "df1.csv")
export(df2, "df2.csv")
....
export(df17, "df17.csv)

I've read a few other posts that say this is an excel problem, and that makes sense, but given the number of data files and amount of data, as well as the need for the client to be able to open it in excel, I need a way to do it on the front end in R. Any ideas?我看过其他几篇帖子说这是一个excel的问题,这说得通,但是考虑到数据文件的数量和数据量,以及客户端需要能够在excel打开它,我需要一种方法在 R 的前端执行此操作。有什么想法吗?

Yes, this is definitely an Excel problem!是的,这绝对是一个Excel的问题!
To demonstrate, In Excel enter your column values save the file as a CSV value and then re-open it in Excel, the leading zeros will disappear.为了演示,在 Excel 中输入您的列值,将文件保存为 CSV 值,然后在 Excel 中重新打开它,前导零将消失。

One option is add a leading non-numerical character such as '一种选择是添加前导非数字字符,例如 '

paste0("\' ", df$col)

Not a great but an option.不是很好,而是一个选择。

A slightly better option is to paste Excel's Text function to the character string.稍微好一点的选择是将 Excel 的文本 function 粘贴到字符串中。 Then Excel will process the function when the function is opened.然后Excel会在function打开的时候处理function。

df$col <- paste0("=Text(", df$col, ", \"000000000000\")")
#or  
df$col <- paste0("=\"", df$col, "\"")
write.csv(df, "df2.csv", row.names = FALSE)

Of course if the CSV file is saved and reopened then the leading 0 will again disappear.当然,如果 CSV 文件被保存并重新打开,那么前导 0 将再次消失。

Another option is to investigate saving the file directly as a.xlsx file with the "writexl", or "XLSX" or similar package.另一种选择是研究将文件直接保存为带有“writexl”或“XLSX”或类似 package 的.xlsx 文件。

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

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