background preloader

File read-write

Facebook Twitter

Parsing CSV with Ruby - Josh Nichols on the Internet. I’m filing this one under “blog posts I wish existed when I was googling.”

Parsing CSV with Ruby - Josh Nichols on the Internet

If you are dealing with data on the web, you are probably most familiar with JSON and XML. Less common nowadays is CSV, but if it’s all you have, and the alternative is screen scraping, then you are thankful. Imagine we have some data in CSV: Year,Make,Model,Description,Price 1997,Ford,E350,"ac, abs, moon",3000.00 1999,Chevy,"Venture ""Extended Edition""","",4900.00 1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00 1996,Jeep,Grand Cherokee,"MUST SELL! Air, moon roof, loaded",4799.00 Now as a Ruby developer, particularly that has been infected by Rails, you’d be able to imagine this as an array of hashes, with keys/values using the column header, as the keys symbolized, and the values converted to numerics and blank ones converted to nil: It might be tempting to just use regular expressions or read each line and split(','), but there are many nuances to the CSV format.

Csv = CSV.new(body) Even warmer! A Guide to the Ruby CSV Library, Part II. Good job, son!

A Guide to the Ruby CSV Library, Part II

– Uncle Bob (if you’re confused who Uncle Bob is you might want to read Part 1 of this tutorial) Uncle Bob is happy with the work we’ve done so far. But there’s one thing left…Remember this file? New employees are confused, what does each column mean? What is 34 or 2548? Now, let’s go and import our file into Ruby (suppose its name is ‘guests.csv’): The CSV library does not possess magic powers so it has no idea that the first row here is a header. Let’s tell the CSV library to NOT treat the first row like any of the rest. A Guide to the Ruby CSV Library, Part I. Several weeks ago I needed to do something in Ruby that involved processing a large number of CSV files.

A Guide to the Ruby CSV Library, Part I

I was happy to learn there was a good, comprehensive CSV library to make my job easier. One thing struck me, however, as soon as I started looking for tutorials: none of them covered this library in detail. Most articles I have seen barely touched the surface of what the CSV library can do. I felt determined to get a solid understanding of this part of Ruby, so I started reading every possible tutorial and book chapter on CSV.

This two-part series is the result of my endeavors. I make some basic assumptions in this article: Parsing csv file in Ruby. CSV Example: Parsing CSV. You can see the complete code that goes along with this article here, or download a zip file.

CSV Example: Parsing CSV

A rather common file format for tabular data is the CSV, or Comma Separated Value, file. It's especially common as an interchange format, every spreadsheet program out there is bound to support CSV files. And for good reason, they're dead simple. Each row is just a comma-separated list of values, and the first row is usually a comma-separated list of column names. They're also ancient, one of the first widely-used file formats in the computing (back when it really was "computing") industry.

Ruby Open: Opening a File in Ruby with Different Modes. Ruby was designed by a Japanese programmer named Yukihiro “Matz” Matsumoto in the 90s.

Ruby Open: Opening a File in Ruby with Different Modes

Ruby, which is based on languages like Perl, Smalltalk, Ada, Lisp and Eiffel, has steadily grown in popularity since then. It was designed to be simple to understand and implement, even for new programmers. Ruby has an automatic memory management system as well as a dynamic type system. The Ruby script was meant to let you do more with less, and there was an emphasis on building a language that supported multiple programming paradigms. It was also designed to run on all possible platforms- you can run it on Windows, Linux and Mac OS X. A Ruby write to file example.

By Alvin Alexander.

A Ruby write to file example

Last updated: Feb 8, 2014. File input/output. When dealing with delimited files, such as comma-delimited text files, it's more convenient to read the file line by line.

File input/output

The readlines method can draw in all the content and automatically parse it as an array, splitting the file contents by the line breaks. The method readline on the other hand, reads a singular line. Again, each read operation moves the file handle forward in the file. If you keep calling readline until you hit the end of the file and then call it again, you'll get an "end of file" error. The File class (more specifically, the IO class that File inherits from) contains the eof? The readline method seems to require more upkeep than readlines. Because the latter loads the entire file at once into memory. The readline method may require a couple more lines of code, but it's more efficient in most scenarios when extracting something from each line; you aren't operating on the entire file contents at once, and you don't need to store the entirety of each line either.

Learn Ruby. If you did the Study Drills from the last exercise you should have seen all sorts of commands (methods/functions) you can give to files.

Learn Ruby

Here's the list of commands I want you to remember: close -- Closes the file. Like File->Save.. in your editor.read -- Reads the contents of the file. You can assign the result to a variable.readline -- Reads just one line of a text file.truncate -- Empties the file. Watch out if you care about the file.write('stuff') -- Writes "stuff" to the file.

For now these are the important commands you need to know. Let's use some of this to make a simple little text editor: Pass variables to Ruby script via command line. Read Write Files in Ruby. Let's look at how we can read / write to a text file with the help of a simple program p027readwrite.rb File.open('p014constructs.rb', 'r') do |f1| while line = f1.gets puts line end end File.open('test.rb', 'w') do |f2| f2.puts "Created by Satish\nThank God!

Read Write Files in Ruby

" end # p027readwrite.rb # Open and read from a text file # Note that since a block is given, file will # automatically be closed when the block terminates File.open('p014constructs.rb', 'r') do |f1| while line = f1.gets puts line end end # Create a new file and write to it File.open('test.rb', 'w') do |f2| # use "\n" for two lines of text f2.puts "Created by Satish\nThank God!

" Ruby Primer - Using the `File` Class. Now we'll take a look at some methods to read from an I/O stream.

Ruby Primer - Using the `File` Class

In these examples, our I/O stream is a file, but remember: as described in the previous lesson, files behave just like any other I/O stream. The File#read method accepts two optional arguments: length, the number of bytes upto which the stream will be read, and buffer, where you can provide a String buffer which will be filled with the file data. Learn Ruby. File input/output. A Ruby write to file example. File Access. Ruby Open: Opening a File in Ruby with Different Modes. What are all the common ways to read a file in Ruby? What are all the common ways to read a file in Ruby? How to write to file in Ruby?