Working Files & Directories / Using CSV
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
In this video we're going to take a look at how to read and write to CSV files. Now CSV is a common format that's used out there for holding data; it's comma separated files, and here you see a very simple little program to write ah, CSV files out. The first thing we have to require the CSV library in order to use it, and then we use the class method open, it's very similar to the file so you can guess that's probably has some file attributes to it. We give it a name and the write flag, and then we have our block and we write out some data. And this is just a little bit different method; it's more akin to how the IO works on C++ but it works just as well with ah, Ruby, so you can do it this way. So I go ahead and run this, and our program exits, and if we go and look in our directory, now we have this data dot CSV. If I go into a program that can read CSV files like Neo Office here will read those types of flies, and navigate into that directory here, and it's going to see that, and actually you can tell it open up, and it's going to say okay, how's it separated, and it's already showing me down here that, oh, hey, I can see this data in here and we bring it in and there's our data. And we can play around with it, and write it out, and do other things to it. But um, what if we wanted to do something, or read, actually, data in. So, let's, let's do that. I'm just going to make up some little spreadsheet kind of thing here, and um, with some varying data in it. Maybe this is data that you've, you've collected, or something along those lines. Let me get in here and save this back into that same directory, and I'm going to call it more data dot CSV, save it out, actually I've got to change my type here to CSV, there we go. Okay, so we're saved out. There's a file. If we open it up in like ah, Text Mate here, or Text Editor you can see there's our data, and I have a little program already I created to read it, it's 1206 read, so the write one was 1206 dash write, this one is 1206 dash read, again it requires CSV, and this one says, prints out a little prompt, enter CSV file to print, and goes out and from the command line, we type in a name, gets that name, cuts off the, the carriage return on the end, and then goes ahead and opens that CSV file up for reading, and then all it does is just prints it back to the screen. And the P is just a shortcut for the PutS, we could do the PutS also. So, let's go ahead and run over to our command line and run that program, and I believe it's more data, data dot CSV, and there we go. It read through our data and printed it out. Now there is a difference between, this prints out when we have an array type structure in here, this prints out that array structure in a little bit cleaner, ah, format like this so there's a little distinction between the two but essentially you get the output here and really we're not worried about what, what this is doing, I just wanted to show you how we can read this in. And you can do the same thing by reading the CSV into an array type structure and then you'd have it your program to manipulate and do whatever you'd like to it. So that's a little bit how to play with CSV files in Ruby. [00:04:08
Tutorial Information
| Course: | Programming With Ruby |
| Author: | Al Anderson |
| SKU: | 33788 |
| ISBN: | 1-934743-01-1 |
| Release Date: | 2007-08-21 |
| Duration: | 8.5 hrs / 113 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 