in reply to Retaining leading Zeros in csv file

  1. Never use split for CSV files. Use Text::CSV_XS or Text::CSV
  2. Viewing data with M$-Excel is not a way of proving any conversion or scanning to be successful, as Excel is more likely to alter your data than your perl program
  3. Use tools like csv2xls to convert CSV to XLS
  4. Leading zeroes get lost when you use the scalar in numeric context, like checking against a number

Enjoy, Have FUN! H.Merijn
  • Comment on Re: Retaining leading Zeros in csv file

Replies are listed 'Best First'.
Re^2: Retaining leading Zeros in csv file
by psini (Deacon) on Jul 02, 2008 at 08:40 UTC

    Expanding point #2: MSExcel does trim leading zeros from any field data recognized as number (i.e. can be converted to a number) when reading from a CSV file.

    It is a very annoying "feature" and I never found a way to disable it. The only "solution" I found is renaming the .csv file to .txt to prevent automatic parsing, importing it as a text delimited file and then forcing all column to text mode.

    If you need to generate a file from Perl to be used by Excel I strongly suggest Spreadsheet::WriteExcel

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

      Thx for the suggestions.. seems nothing can be done from coding side to overcome the above excel functionality ...i will consider renaming it into .txt and then importing from excel ....