d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Beginner Ruby Help!
Add Reply New Topic New Poll
Member
Posts: 1,999
Joined: May 29 2010
Gold: 990.00
Jan 28 2015 07:26pm
I'm trying to write a function to parse a CSV file, but I'm having trouble with some exceptions. I cannot use the CSV class, as that defeats the purpose.

The function has two parameters: delim and file_name, which provide the delimiter for the fields of the file and the file path respectively.

I've gotten the elements to almost the right format, but I'm struggling with special cases such as:

1) A comma inside a string field such as: "Dough, John" which should come out as "Dough, John", but instead I get two elements "Dough" and "John"

2) I can't seem to incorporate the empty elements, such as input: 1, 3, should produce the result ["1", "3", "" ], but I get ["1", "3"]

Please help! Anything would be appreciated.

The following is what I've currently been working on:

Code

def parse_CSV_file(delim, file_name)
fields = []
file = File.open(file_name, "r")
file.each_line do |line|
line = line.chomp
fields.push(line.split(delim))
end

p fields
end


This post was edited by Cattotonic on Jan 28 2015 07:29pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 28 2015 07:36pm
Quote (Cattotonic @ Jan 28 2015 08:26pm)

2) I can't seem to incorporate the empty elements, such as input: 1, 3, should produce the result ["1", "3", "" ], but I get ["1", "3"]


have you read the documentation?

http://www.ruby-doc.org/core-2.2.0/String.html#method-i-split

Quote
split(pattern=$;, [limit]) → anArray
Divides str into substrings based on a delimiter, returning an array of these substrings.

If pattern is a String, then its contents are used as the delimiter when splitting str. If pattern is a single space, str is split on whitespace, with leading whitespace and runs of contiguous whitespace characters ignored.

If pattern is a Regexp, str is divided where the pattern matches. Whenever the pattern matches a zero-length string, str is split into individual characters. If pattern contains groups, the respective matches will be returned in the array as well.

If pattern is omitted, the value of $; is used. If $; is nil (which is the default), str is split on whitespace as if ` ‘ were specified.

If the limit parameter is omitted, trailing null fields are suppressed. If limit is a positive number, at most that number of fields will be returned (if limit is 1, the entire string is returned as the only entry in an array). If negative, there is no limit to the number of fields returned, and trailing null fields are not suppressed.

When the input str is empty an empty Array is returned as the string is considered to have no fields to split.

" now's the time".split #=> ["now's", "the", "time"]
" now's the time".split(' ') #=> ["now's", "the", "time"]
" now's the time".split(/ /) #=> ["", "now's", "", "the", "time"]
"1, 2.34,56, 7".split(%r{,\s*}) #=> ["1", "2.34", "56", "7"]
"hello".split(//) #=> ["h", "e", "l", "l", "o"]
"hello".split(//, 3) #=> ["h", "e", "llo"]
"hi mom".split(%r{\s*}) #=> ["h", "i", "m", "o", "m"]

"mellow yellow".split("ello") #=> ["m", "w y", "w"]
"1,2,,3,4,,".split(',') #=> ["1", "2", "", "3", "4"]
"1,2,,3,4,,".split(',', 4) #=> ["1", "2", "", "3,4,,"]
"1,2,,3,4,,".split(',', -4) #=> ["1", "2", "", "3", "4", "", ""]


"".split(',', -1) #=> []
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 28 2015 07:44pm
Quote (Cattotonic @ Jan 28 2015 08:26pm)

1) A comma inside a string field such as: "Dough, John" which should come out as "Dough, John", but instead I get two elements "Dough" and "John"


are they inside quotes inside your data? instead of breaking directly on commas, you can use a regex instead of the string delimiter "," that's schmart enough to only grab commas not inside quotes. refer to regex tutorials if you're unfamiliar with it. or just tackle the problem in multiple steps and manually break it if you can't get the regex to work.
Member
Posts: 50,343
Joined: Apr 3 2008
Gold: 0.00
Jan 28 2015 10:05pm
Code
data = 'Carlos,22,4352,"I am ""pleased"", but could be better"'

positions = data.enum_for(:scan, /(,)(?=(?:[^"]|"[^"]*")*$)/).map {Regexp.last_match.begin(0) }
positions.push(-0)

lastPosition = 0
positions.each do |position|
puts data[lastPosition..position-1]
lastPosition = position+1
end

#output

#Process started >>>
#Carlos
#22
#4352
#"I am ""pleased"", but could be better"
#<<< Process finished. (Exit code 0)


Brought to you by your favorite banned duck.

This post was edited by Rikuo on Jan 28 2015 10:07pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jan 28 2015 11:05pm
Quote (Rikuo @ Jan 28 2015 11:05pm)
Code
data = 'Carlos,22,4352,"I am ""pleased"", but could be better"'

positions = data.enum_for(:scan, /(,)(?=(?:[^"]|"[^"]*")*$)/).map {Regexp.last_match.begin(0) }
positions.push(-0)

lastPosition = 0
positions.each do |position|
puts data[lastPosition..position-1]
lastPosition = position+1
end

#output

#Process started >>>
#Carlos
#22
#4352
#"I am ""pleased"", but could be better"
#<<< Process finished. (Exit code 0)


Brought to you by your favorite banned duck.


Abduck?

if he was a doctor, would he be a quack?

Member
Posts: 1,999
Joined: May 29 2010
Gold: 990.00
Jan 29 2015 02:03pm
So I have resorted to use regex for elements with quotes, but if they are double quotes they should be included in the result, i.e. ""bob"' should be presented as "bob".

Furthermore, I am stuck on parsing the lines that have strings, as I am not certain how to parse the strings separate from the other elements. The delimiter can be any character other than ", so I should parse the string first then check if there is a delimiter after. But how can that be done with a regex split? Is there a way to split up the string based on matching parts?
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll