[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

csv updater

Geoff

3/16/2006 11:25:00 PM

Greetings!

I got some help on this a while back and have been tweaking on it
trying to get it do work right. What I want to do is take a csv file,
and whenever there is a blank cell update it with the info from the
cell directly above (and on down as long as there are blanks).

There are two problems with what I have so far:

1. It does not retain the double quotes and I would like it to be able
to do that.
2. On a previous version it was working for just the last column. I
have tried to adjust it to make it work for any column (at least that I
list in the write(colNum) definition, but it is not working.

<<begin code>>

require 'CSV'

class BlankFiller
def initialize(last='')
@last = last
end

def fill(e)
if e.empty?
e = @last
else
@last = e
end
end
end

blank_filler = BlankFiller.new
out_csv = CSV.open('C:\temp\geoff\filldown\filldownNew.txt', 'w')

class ColumnWrite < BlankFiller
def write(colNum)
CSV.foreach("C:\\temp\\geoff\\filldown\\filldown.txt") do |row|
row[colNum] = blank_filler.fill(row[colNum])
out_csv << row
end
write(0)
write(1)
write(2)
end
end

<<end code>>

Here is the csv file I'm working with for a test case:

"BegDoc","EndDoc","New"
"Doc1BegDoc","Doc1EndDoc","Test1"
"Doc2BegDoc","",""
"Doc2BegDoc","",""
"Doc3BegDoc","Doc3EndDoc","Test2"
"Doc4BegDoc","",""
"Doc5BegDoc","Doc5EndDoc","New"

Ideas?

Thanks!

Geoff

7 Answers

James Gray

3/17/2006 4:50:00 AM

0

On Mar 16, 2006, at 5:28 PM, Geoff wrote:

> 1. It does not retain the double quotes and I would like it to be able
> to do that.
> 2. On a previous version it was working for just the last column. I
> have tried to adjust it to make it work for any column (at least
> that I
> list in the write(colNum) definition, but it is not working.

I'm pretty sure this does what you want:

Neo:~/Desktop$ ls
csv_filldown.rb test_data.csv
Neo:~/Desktop$ cat test_data.csv
"BegDoc","EndDoc","New"
"Doc1BegDoc","Doc1EndDoc","Test1"
"Doc2BegDoc","",""
"Doc2BegDoc","",""
"Doc3BegDoc","Doc3EndDoc","Test2"
"Doc4BegDoc","",""
"Doc5BegDoc","Doc5EndDoc","New"
Neo:~/Desktop$ cat csv_filldown.rb
#!/usr/local/bin/ruby -w

require "csv"
require "enumerator"

last_row = Array.new

CSV.foreach(ARGV.shift) do |row|
puts( row.enum_for(:each_with_index).map do |cell, index|
if cell.empty?
last_row[index]
else
last_row[index] = cell
end
end.map { |output| "\"#{output}\""}.join(",") )
end
Neo:~/Desktop$ ruby csv_filldown.rb test_data.csv > output.csv
Neo:~/Desktop$ cat output.csv
"BegDoc","EndDoc","New"
"Doc1BegDoc","Doc1EndDoc","Test1"
"Doc2BegDoc","Doc1EndDoc","Test1"
"Doc2BegDoc","Doc1EndDoc","Test1"
"Doc3BegDoc","Doc3EndDoc","Test2"
"Doc4BegDoc","Doc3EndDoc","Test2"
"Doc5BegDoc","Doc5EndDoc","New"

Hope that helps.

James Edward Gray II



Geoff

3/17/2006 5:38:00 PM

0

Cool, the puts command works, but when I tweak it to output this to a
file, the result is:

78,101,119
84,101,115,116,49
84,101,115,116,49
84,101,115,116,49
84,101,115,116,50
84,101,115,116,50
78,101,119

The tweak I used on the code is:

require "csv"
require "enumerator"

last_row = Array.new
out_csv = CSV.open("C:\\temp\\geoff\\filldown\\filldownNew.txt", "w")
output = ''
CSV.foreach("C:\\temp\\geoff\\filldown\\filldown.txt") do |row|
puts( row.enum_for(:each_with_index).map do |cell, index|
if cell.empty?
last_row[index]
else
last_row[index] = cell
end
end.map { |output| "\"#{output}\""}.join(",") )
out_csv << output
end

I'm getting the feeling I just don't understand programming and Ruby
enough at this point and need to re-read some intro material. Thanks a
bunch for spending time on this problem for me, I do appreciate it!

James Gray

3/17/2006 6:22:00 PM

0

On Mar 17, 2006, at 11:38 AM, Geoff wrote:

> Cool, the puts command works, but when I tweak it to output this to a
> file...

If you go back and look at my last message, I used the puts version
to create a file (using redirection). That's a pretty flexible
trick, probably worth getting the hang of.

The main problem with your new version is that you are using CSV to
manage the output file, but it order to enforce quoting we had to
roll our own solution (the CSV file format does not require it for
the examples you are showing). Switching to a normal file should get
you going:

#!/usr/local/bin/ruby -w

require "csv"
require "enumerator"

last_row = Array.new

File.open("C:\\temp\\geoff\\filldown\\filldownNew.txt", "w") do |
out_file|
CSV.foreach("C:\\temp\\geoff\\filldown\\filldown.txt") do |row|
out_file.puts( row.enum_for(:each_with_index).map do |cell, index|
if cell.empty?
last_row[index]
else
last_row[index] = cell
end
end.map { |output| "\"#{output}\""}.join(",") )
end
end

Hope that helps.

James Edward Gray II



Geoff

3/17/2006 6:34:00 PM

0

>>If you go back and look at my last message, I used the puts version
to create a file (using redirection). That's a pretty flexible
trick, probably worth getting the hang of.

I'll have to look into that. When I run that code it does not modify my
file, just puts to the shell. Sounds like a good trick, which I'll
certainly look into.

>>The main problem with your new version is that you are using CSV to
manage the output file, but it order to enforce quoting we had to
roll our own solution (the CSV file format does not require it for
the examples you are showing). Switching to a normal file should get
you going:

Ah.. works perfectly!

Thanks a ton for your help on this! I think I've learned more by trying
to solve this problem than I have the whole time reading tutorials and
such.

Geoff

Michael Ejercito

10/12/2013 5:45:00 PM

0



"The Revd" wrote in message
news:kcdh59tp51if5dlelq0ga8u62lhae82rsb@4ax.com...

>On Fri, 11 Oct 2013 19:10:49 -0700, "Michael Ejercito"
><mejercit@hotmail.com> wrote:

>>
>>
>>"The Peeler" wrote in message news:DEX5u.6234$RZ.2999@fx08.fr7...
>>
>>>On Fri, 11 Oct 2013 07:54:58 -0700, The Rectum, the resident psychopath
>>>of
>>>sci, impersonating his master "The Revd", wrote:
>>
>>><FLUSH most of the usual idiotic and psychotic Grik garbage>
>> If 9/11 survivors and the family members of the killed ever read what
>> he
>>wrote about how my country, the United States of America

>Your country is the Flippines and your loyalty is to 'Israel'.
Nithing, my loyalty is to my country, the United States of America.

> You
>have NO loyalty at ALL to your host country. You support your host
>country's Number One enemy, 'Israel'. YOU are a TRAITOR, gook!
I am loyal to America.

>> deserved the attack
>>for sucking "the collective jew rectum", they would whip his nithing hide.

>It DID deserve it, gook....and it continues to deserve it.
>If the associates of the 7/11 perpetrators ever caught a jew rectum
>sucking gook like you, they would skin you alive and crucify you.
They hate Americans as well as Jews.

They are driven by Judenhass.
>>>>
>>>> I'm devastated, gook.
>>
>>>Indeed, you are! You FINALLY said something truthful! <BG>

><snigger>

>> He is devastated through his choices and conduct.

>YOU are devastated by your subhuman DNA, gook!
Nothing about me is subhuman.

>> The mangina is explained by the following link.
>>
>>
>> http://nizkor.org/ftp.cgi/people/ftp.py?people//m/morton.chris/what...

>Morton?
Yes.

> Chrissie Morton???
Yes.

>The uppity coon from Ohio that I kicked
>off this group years ago?????? LOL!
He kicked your nithing ass all over Usenet.

The following article explains your pathology.

History's oldest hatred

by Jeff Jacoby
The Boston Globe
March 11, 2009

http://www.jeffjacoby.com/4743/historys-old...

ANTI-SEMITISM is an ancient derangement, the oldest of hatreds, so it
is strange that it lacks a more meaningful name. The misnomer "anti-
Semitism" -- a term coined in 1879 by the German agitator Wilhelm
Marr, who wanted a scientific-sounding euphemism for Judenhass, or
Jew-
hatred -- is particularly inane, since hostility to Jews has never had
anything to do with Semites or being Semitic. (That is why those who
protest that Arabs cannot be anti-Semitic since "Arabs are Semites
too" speak either from ignorance or disingenuousness.)

Perhaps there is no good name for a virus as mutable and unyielding as
anti-Semitism. "The Jews have been objects of hatred in pagan,
religious, and secular societies," write Joseph Telushkin and Dennis
Prager in Why the Jews?, their classic study of anti-Semitism.
"Fascists have accused them of being Communists, and Communists have
branded them capitalists. Jews who live in non-Jewish societies have
been accused of having dual loyalties, while Jews who live in the
Jewish state have been condemned as 'racists.' Poor Jews are bullied,
and rich Jews are resented. Jews have been branded as both rootless
cosmopolitans and ethnic chauvinists. Jews who assimilate have been
called a 'fifth column,' while those who stay together spark hatred
for remaining separate."

So hardy is anti-Semitism, it can flourish without Jews. Shakespeare's
poisonous depiction of the Jewish moneylender Shylock was written for
audiences that had never seen a Jew, all Jews having been expelled
from England more than 300 years earlier. Anti-Semitic bigotry infests
Saudi Arabia, where Jews have not dwelt in at least five centuries;
its malignance is suggested by the government daily Al-Riyadh, which
published an essay claiming that Jews have a taste for "pastries mixed
with human blood."

Esther Confounding Haman (Engraving by Gustave Dor???, 1875)
There was Jew-hatred before there was Christianity or Islam, before
Nazism or Communism, before Zionism or the Middle East conflict. This
week Jews celebrate the festival of Purim, gathering in synagogues to
read the biblical book of Esther. Set in ancient Persia, it tells of
Haman, a powerful royal adviser who is insulted when the Jewish sage
Mordechai refuses to bow down to him. Haman resolves to wipe out the
empire's Jews and makes the case for genocide in an appeal to the
king:

"There is a certain people scattered and dispersed among ... all the
provinces of your kingdom, and their laws are different from those of
other peoples, and the king's laws they do not keep, so it is of no
benefit for the king to tolerate them. If it please the king, let it
be written that they be destroyed." After winning royal assent, Haman
makes plans "to annihilate, to kill and destroy all the Jews, the
young and the elderly, children and women, in one day . . . and to
take their property for plunder."

What drives such bloodlust? Haman's indictment accuses the Jews of
lacking national loyalty, of insinuating themselves throughout the
empire, of flouting the king's law. But the Jews of Persia had done
nothing to justify Haman's murderous anti-Semitism -- just as Jews in
later ages did nothing that justified their persecution under the
Church or Islam, or their expulsion from so many lands in Europe and
the Middle East, or their repression at the hands of Russian czars and
Soviet commissars, or their slaughter by Nazi Germany. When the
president of Iran today calls for the extirpation of the Jewish state,
when a leader of Hamas vows to kill Jewish children around the world,
when firebombs are hurled at synagogues in London and Paris and
Chicago, it is not because Jews deserve to be victimized.

Some Jews are no saints, but the paranoid frenzy that is anti-Semitism
is not explained by what Jews do, but by what they are. The Jewish
people are the object of anti-Semitism, not its cause. That is why the
haters' rationales can be so wildly inconsistent and their agendas so
contradictory. What, after all, do those who vilify Jews as greedy
bankers have in common with those who revile them as seditious
Bolsheviks? Nothing, save an irrational obsession with Jews.

At one point in the book of Esther, Haman lets the mask slip. He
boasts to his friends and family of "the glory of his riches, and the
great number of his sons, and everything in which the king had
promoted him and elevated him." Still, he seethes with rage and
frustration: "Yet all this is worthless to me so long as I see
Mordechai the Jew sitting at the king's gate." That is the
unforgivable offense: "Mordechai the Jew" refuses to blend in, to
abandon his values, to be just like everyone else. He goes on sitting
there -- undigested, unassimilated, and for that reason unbearable.

Of course Haman had his ostensible reasons for targeting Jews. So did
Hitler and Arafat; so does Ahmadinejad. Sometimes the anti-Semite
focuses on the Jew's religion, sometimes on his laws and lifestyle,
sometimes on his national identity or his professional achievements.
Ultimately, however, it is the Jew's Jewishness, and the call to
higher standards that it represents, that the anti-Semite cannot
abide.

With all their flaws and failings, the Jewish people endure, their
role in history not yet finished. So the world's oldest hatred endures
too, as obsessive and indestructible -- and deadly -- as ever.

(Jeff Jacoby is a columnist for The Boston Globe.)

The Revd

10/12/2013 6:02:00 PM

0

On Sat, 12 Oct 2013 10:44:42 -0700, "Michael Ejercito"
<mejercit@hotmail.com> wrote:

>
>
>"The Revd" wrote in message
>news:kcdh59tp51if5dlelq0ga8u62lhae82rsb@4ax.com...
>
>>On Fri, 11 Oct 2013 19:10:49 -0700, "Michael Ejercito"
>><mejercit@hotmail.com> wrote:
>
>>>
>>>
>>>"The Peeler" wrote in message news:DEX5u.6234$RZ.2999@fx08.fr7...
>>>
>>>>On Fri, 11 Oct 2013 07:54:58 -0700, The Rectum, the resident psychopath
>>>>of
>>>>sci, impersonating his master "The Revd", wrote:
>>>
>>>><FLUSH most of the usual idiotic and psychotic Grik garbage>
>>> If 9/11 survivors and the family members of the killed ever read what
>>> he
>>>wrote about how my country, the United States of America
>
>>Your country is the Flippines and your loyalty is to 'Israel'.
> Nithing, my loyalty is to my country, the United States of America.

Gook, your ONLY loyalty is to Israpundit and 'Israel'. You are a
TRAITOR to your host country.

>> You
>>have NO loyalty at ALL to your host country. You support your host
>>country's Number One enemy, 'Israel'. YOU are a TRAITOR, gook!
> I am loyal to America.

You are ONLY loyal to 'Israel'. And since 'Israel' and America are
sworn enemas, you cannot be loyal to America.

>>> deserved the attack
>>>for sucking "the collective jew rectum", they would whip his nithing hide.
>
>>It DID deserve it, gook....and it continues to deserve it.
>>If the associates of the 7/11 perpetrators ever caught a jew rectum
>>sucking gook like you, they would skin you alive and crucify you.
> They hate Americans as well as Jews.

Of course they do. America sucks jew rectums wholesale!

> They are driven by Judenhass.

YOU are driven by Judenliebe.

>>>>> I'm devastated, gook.
>>>
>>>>Indeed, you are! You FINALLY said something truthful! <BG>
>
>><snigger>
>
>>> He is devastated through his choices and conduct.
>
>>YOU are devastated by your subhuman DNA, gook!
> Nothing about me is subhuman.

As a craven gook, you are subhuman BY BIRTH.

The Revd

10/12/2013 8:11:00 PM

0

On Sat, 12 Oct 2013 11:48:57 -0700, "Michael Ejercito"
<mejercit@hotmail.com> wrote:

>
>
>"The Revd" wrote in message
>news:rf3j591qnvi4half1lj4rjsn8mpp3tsm43@4ax.com...
>
>O>n Sat, 12 Oct 2013 10:44:42 -0700, "Michael Ejercito"
>><mejercit@hotmail.com> wrote:
>
>>>
>>>
>>>"The Revd" wrote in message
>>>news:kcdh59tp51if5dlelq0ga8u62lhae82rsb@4ax.com...
>>>
>>>>On Fri, 11 Oct 2013 19:10:49 -0700, "Michael Ejercito"
>>>><mejercit@hotmail.com> wrote:
>>>>> If 9/11 survivors and the family members of the killed ever read
>>>>> what
>>>>> he
>>>>>wrote about how my country, the United States of America
>>>
>>>>Your country is the Flippines and your loyalty is to 'Israel'.
>>> Nithing, my loyalty is to my country, the United States of America.
>
>>Gook, your ONLY loyalty is to Israpundit and 'Israel'. You are a
>>TRAITOR to your host country.
> Mangina, I am loyal.

Gook, you are a TRAITOR to your host country.

>
>>>> You
>>>>have NO loyalty at ALL to your host country. You support your host
>>>>country's Number One enemy, 'Israel'. YOU are a TRAITOR, gook!
>>> I am loyal to America.
>
>>You are ONLY loyal to 'Israel'.
> I am loyal to America.

You are ONLY loyal to 'Israel', America's Number One enema.

>> And since 'Israel' and America are
>>sworn enemas, you cannot be loyal to America.
> They are not enemies.

They are sworn enemas. Their objectives are 180 degrees apart.

>
> Israel is our ally.

We're talking about America not the fucking Flippines.

>>>>> deserved the attack
>>>>>for sucking "the collective jew rectum", they would whip his nithing
>>>>>hide.
>>>
>>>>It DID deserve it, gook....and it continues to deserve it.
>>>>If the associates of the 7/11 perpetrators ever caught a jew rectum
>>>>sucking gook like you, they would skin you alive and crucify you.
>>> They hate Americans as well as Jews.
>
>>Of course they do. America sucks jew rectums wholesale!
> Most of us Americans would beat your girlyboy hide like a drum if they
>heard you say that.

"Us Americans"??? LOL! Most Americans would skin you alive for
pretending to be an American, gook!
Anyway, they heard it. What the fuck are they gonna [sic] do about
it, gook?

>
> Sharon Roth dedicated unto you this poem.

Greek Sharon Roth and greek you, gook.