Update a field in CSV file using fastercsv

Hello everyone,

Is it posssible to update a field of CSV file using Fastercsv? For
example:
change a row [1,2,3,4] to [10,2,3,4].

Thanks,

Li

On Sep 19, 2008, at 10:41 PM, Li Chen wrote:

Is it posssible to update a field of CSV file using Fastercsv? For
example:
change a row [1,2,3,4] to [10,2,3,4].

Sure. You would do this as you would change any file. Here are the
steps:

  1. Open the existing data for reading
  2. Open a different file for writing
  3. Copy over the content making changes as needed
  4. Rename the new file to replace the old file

Hope that helps.

James Edward G. II

James G. wrote:

On Sep 19, 2008, at 10:41 PM, Li Chen wrote:

Is it posssible to update a field of CSV file using Fastercsv? For
example:
change a row [1,2,3,4] to [10,2,3,4].

Sure. You would do this as you would change any file. Here are the
steps:

  1. Open the existing data for reading
  2. Open a different file for writing
  3. Copy over the content making changes as needed
  4. Rename the new file to replace the old file

Hope that helps.

James Edward G. II

Hi James,

Thank you very much.

One more question: Is this the same idea behind what we do with ‘save’
or ‘save as’ in microsofte WORD or EXCEL?

Li

James G. wrote:

On Sep 20, 2008, at 4:19 PM, Li Chen wrote:

  1. Open the existing data for reading
    Thank you very much.

One more question: Is this the same idea behind what we do with ‘save’
or ‘save as’ in microsofte WORD or EXCEL?

I’m not sure I completely understand this question. It is similar to
how most programs save data though, minus some complications.

James Edward G. II

Thanks,

Li

On Sep 20, 2008, at 4:19 PM, Li Chen wrote:

  1. Open the existing data for reading
    Thank you very much.

One more question: Is this the same idea behind what we do with ‘save’
or ‘save as’ in microsofte WORD or EXCEL?

I’m not sure I completely understand this question. It is similar to
how most programs save data though, minus some complications.

James Edward G. II

On Sat, Sep 20, 2008 at 5:19 PM, Li Chen [email protected] wrote:

  1. Open the existing data for reading
    Thank you very much.

One more question: Is this the same idea behind what we do with ‘save’
or ‘save as’ in microsofte WORD or EXCEL?

An atomic save, as James suggested by the 4 steps above, is
essentially the same as a “Save” in Word or Excel.

A Save is just two “Save As” calls.

I’ve got a file called “A”
I make some changes to it, then
I save it as “B”
Once I am sure that my changes to “B” are okay, I save it as “A”, to
replace the original file.

The reason we need to do things this way is that (at least in Ruby,
but in most languages I’d imagine), there isn’t really a way to make a
change to a file while you’re reading it.

So we read in the file, writing another file with our changes.
Then when we’re done reading and writing, we replace the old file with
our new one. Conceptually, this ends up accomplishing the same thing,
it lets us make changes to a single file. The second file is just a
temporary file for doing work on.

Make sense?

-greg

2008/9/21 Gregory B. [email protected]:

The reason we need to do things this way is that (at least in Ruby,
but in most languages I’d imagine), there isn’t really a way to make a
change to a file while you’re reading it.

Sorry, Greg, but that’s not correct. See the following example.

Create a file with three lines:

name = “test.txt”

File.open(name, “w”) do |file|
3.times do |num|
file.puts “line #{num}”
end
end

Check the contents of the file:

puts File.read(name)

Output:

line 0
line 1
line 2

Now read a line, write something, and read a line again:

File.open(name, “r+”) do |file|
puts file.gets
file.puts “new data”
puts file.gets
end

Output:

line 0
ne 2

Read the file again:

puts File.read(name)

Output:

line 0
new data
ne 2

Regards,
Pit

Gregory B. wrote:

On Sat, Sep 20, 2008 at 5:19 PM, Li Chen [email protected] wrote:

  1. Open the existing data for reading
    Thank you very much.

One more question: Is this the same idea behind what we do with ‘save’
or ‘save as’ in microsofte WORD or EXCEL?

An atomic save, as James suggested by the 4 steps above, is
essentially the same as a “Save” in Word or Excel.

A Save is just two “Save As” calls.

I’ve got a file called “A”
I make some changes to it, then
I save it as “B”
Once I am sure that my changes to “B” are okay, I save it as “A”, to
replace the original file.

The reason we need to do things this way is that (at least in Ruby,
but in most languages I’d imagine), there isn’t really a way to make a
change to a file while you’re reading it.

So we read in the file, writing another file with our changes.
Then when we’re done reading and writing, we replace the old file with
our new one. Conceptually, this ends up accomplishing the same thing,
it lets us make changes to a single file. The second file is just a
temporary file for doing work on.

Make sense?

-greg

They really make sense.

Thank you very much,

Li

On Sep 24, 2008, at 7:16 AM, Pit C. wrote:

name = “test.txt”

file.puts “new data”
puts file.gets
end

This has pretty limited use. It’s rare that you can get away with
chopping up a file like this, in my opinion.

James Edward G. II

On Wed, Sep 24, 2008 at 8:16 AM, Pit C. [email protected]
wrote:

2008/9/21 Gregory B. [email protected]:

The reason we need to do things this way is that (at least in Ruby,
but in most languages I’d imagine), there isn’t really a way to make a
change to a file while you’re reading it.

Sorry, Greg, but that’s not correct. See the following example.

I’ve always wondered how r+ is used, but I guess it’s not clear to me
in practice how I’d use it without things getting convoluted.

-greg

On Sep 24, 2008, at 8:53 AM, James G. wrote:

Create a file with three lines:
This has pretty limited use. It’s rare that you can get away with
chopping up a file like this, in my opinion.

James Edward G. II

If you’re dealing with a file containing fixed-size records (even if
they are text), it can be quite useful. For your average text file
which is treated as a collection of “lines” with varying length, this
is never going to be easy. Overwriting is easy; deleting can be
handled by maintaining two positions in the file and cycling reading,
repositioning, writing, and repositioning until truncating the “extra”
bytes that are left at the end; inserting is tough because you have to
keep track of the bytes that don’t fit in the original file until you
reach the end and can append.

That’s why is is usually so much easier to read to memory, make your
changes, and write to a new file.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]