update a field in CSV file using fastercsv

L

Li Chen

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
 
J

James Gray

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 Gray II
 
L

Li Chen

James said:
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 Gray 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
 
J

James Gray

James said:
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 Gray 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?

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

James Edward Gray II
 
L

Li Chen

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

James Edward Gray II


Thanks,

Li
 
G

Gregory Brown

James said:
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 Gray 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?

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
 
L

Li Chen

Gregory said:
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
 
P

Pit Capitain

2008/9/21 Gregory Brown said:
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
 
G

Gregory Brown

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
 
J

James Gray

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

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 Gray II
 
R

Rob Biedenharn

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 Gray 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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top