Monday, February 21, 2011

Working with CSV files using AS3: Part 1

Today we'll learn what is the CSV (comma separated values) format and what is it used for.

Comma separated values is basically a text file, which stores rows of data, where each value is separated with a comma (but it can be any other symbol as well). This simple file format is widely supported, so it is often used to transfer tabular data between different programs that aren't even written in the same language.

For example, we can createa CSV file using Excel. Let's make a quick file with some tabular data in it:



Save this file as "Other format" and select the CSV file extension. Save the file, and try opening it with a simple text editor, for example, Notepad. You will get this (if you're using Windows):

ID,First name,Last name,Job
1,John,Goodman,Driver
2,Henry,Williams,Pharmacist
3,Jimmy,Orange,Godfather
4,Mary,Thompson,Nurse
5,Derrick,Jackson,Police man
6,Clint,Eastwood,Cowboy

As you can see, each cell here is a separate value. Each row is separated by insivisble \r\n symbols on Windows, \n on Linux and \r on Mac. It doesn't depend on which platform is the flash played on - it depends on where has been the CSV file created. If you have an empty value, you still need to have that emptyness surrounded by commas. For example:

7,Dog,,Animal

Say, you need to display some tabular data from an Excel file in a flash file. Then you would store the CSV version of the file on a server, and flash would have to read, parse and display the data. This can be effectively done using Actionscript 3.

We will learn how to load comma separated values using AS3 in the next tutorial!

Thanks for reading!

Related:

Working with CSV files using AS3: Part 2
Working with CSV files using AS3: Part 3
Working with CSV files using AS3: Part 4
Working with CSV files using AS3: Part 5

1 comment:

Post a Comment