# CSV confusion newbie question

**URL:** <https://www.ruby-forum.com/t/csv-confusion-newbie-question/179865>\
**Category:** Ruby\
**Created:** [December 6, 2009, 1:37am UTC](https://www.ruby-forum.com/t/csv-confusion-newbie-question/179865 "2009-12-06T01:37:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [December 6, 2009, 1:37am UTC](https://www.ruby-forum.com/t/csv-confusion-newbie-question/179865/1 "2009-12-06T01:37:15Z")

</div>

I am confused on why my two Ruby scripts using CSV behave differently. I  
hope someone can explain why it works this way and how to get consistent  
results.

My first attempt worked; I parsed a CSV file with double-quoted, comma  
separated input, and sent it via SMTP to our Ticketing System, and it  
worked perfectly. The CSV handling section looks like this, (line  
numbers help show wrapped lines):

37  
38 # Open csv input file Read-only, and assign each line to variables,  
39 # then call email, in turn.  
40  
41 CSV.open(csvfile, ‘r’) do |row|  
42  
qid,cvss\_base,cvss\_temporal,cve\_id,cve\_url,bugtraq\_id,bugtraq\_url,vendor\_id,vendor\_url,last\_update,category,vuln  
\_type,title,diagnosis,solution,consequence,pci\_flag,qualys\_severity =  
row  
43 subject = “QID #{qid} #{title}”

The values were used as in this method:

27 serverport = 25  
28 Net::SMTP.start( mailserver, serverport ) do |smtp|  
29 smtp.open\_message\_stream( sourceaddr, destaddr ) do |f|  
30 f.puts “From: #{sourceaddr}”  
31 f.puts “To: #{destaddr}”  
32 f.puts “Subject: #{subject}”  
33 f.puts body  
34 end  
35 end  
36 end

Now I wanted to take another CSV file, (a list from our Systems  
Database), containing IP addresses, search code (usually a simple  
hostname), one field contains a list which may have IPs, Netmasks, and  
other data. When I tried matching the first IP field, it failed, and  
upon puts’ing the line I found it was still quoted! Next is the entire  
(failing) script:

1 #!/usr/bin/ruby -w  
2 #  
3 # @(#) Usage: parsetest.rb [input.csv]  
4 #  
5 #  
6  
7 require ‘csv’  
8 require ‘net/smtp’  
9  
10 unless ARGV.length == 1  
11 puts “Usage: #{0} csv\_input\_file”  
12 exit  
13 end  
14  
15 # Assign first argument to variable “csvfile”  
16 csvfile = ARGV[0]  
17  
18 CSV.open(csvfile, ‘r’) do |row|  
19  
mgmtip,searchcode,cid,status,ipaddr,otherip,ciowner,description,category,loccat,locparent,loc,rack  
= row  
20 #if mgmtip =~  
/\A(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}\z/  
21 puts “Search code: #{searchcode}, \n\tMgmt\_IP: #{mgmtip}, CID:  
#{cid}, #{status}, #{ciowner}”  
22 #end  
23  
24 end

This is partial output from the script:

$ parsetest.rb testexport.csv  
Search code: “Search code”,  
Mgmt\_IP: “Management IP”, CID: “ID”, “CI Owner Organization  
Search code”, “Other IP Addresses”  
Search code: “OLD-ENV01”,  
Mgmt\_IP: “10.192.72.82”, CID: “206”, “SEESN”, “192.168.1.2”  
Search code: “OLD-ENV02”,  
Mgmt\_IP: , CID: “207”, “SEESN”,  
Search code: “OLD-PUB01C01”,

Thanks for any help, tips, explanation.  
7thWAP,  
total newbie

---

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [December 6, 2009, 2:36am UTC](https://www.ruby-forum.com/t/csv-confusion-newbie-question/179865/2 "2009-12-06T02:36:09Z")

</div>

Michael R. wrote:

> I am confused on why my two Ruby scripts using CSV behave differently. I

Please ignore. I forgot to follow the first rule of data mangling…  
“recheck your source data”. I don’t know what caused it, but the quotes  
were all duplicated.  
\<I’m very embarrassed.\>
