?
Solved

Ruby - delimiter problem

Posted on 2009-02-18
6
Medium Priority
?
406 Views
Last Modified: 2012-08-13
Hello
Trying to change the delimiters in attached file to comma - I should end up with 15 columns seperated by commas.
so far my script is as follows.

f = File.open("C:/RUBY_WORKING_MODELS/data_by_column_access.txt", "r")
lines = f.readlines
lines.each do|line|
line = line.gsub(/(DATA)/,'').gsub(/\" "/,',') 
puts "#{line}"
end

Open in new window

data-by-column-access.txt
0
Comment
Question by:philsivyer
  • 3
  • 3
6 Comments
 
LVL 14

Accepted Solution

by:
wesgarrison earned 1500 total points
ID: 23676246
Looks like you're escaping the first quote in gsub when you don't have to.


line.gsub(/(DATA)/,'').gsub(/\" "/,',') 
 
# should be
 
line.gsub(/(DATA)/,'').gsub(/" "/,',') # without escaping slash

Open in new window

0
 
LVL 14

Expert Comment

by:wesgarrison
ID: 23676297
You'll also have to get rid of the very first and very last quote, since they won't match /" "/, which you can do after you put the commas in.
0
 

Author Comment

by:philsivyer
ID: 23679409
Hello
Still not working as expected - I had a similar problem a while ago where Gertone offered a solution with a different outcome. The script below may give some clues as to the delimiter problem. If you remove row one (headers) from the attached file and run the following script you will see that each record has been delimited correctly - unfortunately, the output is top down i.e. one row for each field.
What I need in this case is row by row with the fileds delimited correctly.
The script.
line = File.open("C:/RUBY_WORKING_MODELS/ears_data_by_column.txt").readlines
line.each do|line|
line = line.gsub(/(DATA)/,'')
line = line.gsub('\"', 'DOUBLEQUOTE')
line = line.gsub(/'\"'/,',').gsub('\r\n', ' ').gsub(/\s+/, ' ')
line = line.scan(/(("[^"]*")|(\d+))/) do |full,a,b|
full = full.inspect.gsub('DOUBLEQUOTE', '\"').gsub(/\\/,'').gsub(/\""/,'"')
puts "#{full}"
end
end

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:philsivyer
ID: 23719460
Hello
Just wondering if any more thoughts on this problem.
0
 
LVL 14

Expert Comment

by:wesgarrison
ID: 23722429
Whoops, I missed your previous comment.

I'll check it out later today if no-one else gets to helping you first.
0
 

Author Closing Comment

by:philsivyer
ID: 31548187
Thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month17 days, 3 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question