Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change … (elipses) to ... (three periods) in Ruby?

I'm parsing this document using nokogiri. I found there are some (elipses) characters in that page and can't be removed. I want to know how to use Ruby to replace all (elipses) to ... (three periods).

BTW, you can search this string to find all …s

Specifies whether ALTER TABLE

Edit: I added my program and the error message.

# encoding: UTF-8
require 'nokogiri'
require 'open-uri'
require 'terminal-table'

def change s
    {Nokogiri::HTML(" ").text => " ", 
     Nokogiri::HTML(""").text => '"',
     Nokogiri::HTML("™").text => '(TM)',
     Nokogiri::HTML("&").text => "&",
     Nokogiri::HTML("&lt;").text => "<",
     Nokogiri::HTML("&gt;").text => ">",
     Nokogiri::HTML("&copy;").text => "(C)",
     Nokogiri::HTML("&reg;").text => "(R)",
     Nokogiri::HTML("&yen;").text => " "}.each do |k, v|
         s.gsub!(k, v)
     end
     s
end

doc = Nokogiri::HTML(open('http://msdn.microsoft.com/en-us/library/ms189782.aspx').read.tr("…","..."))
temp = []
doc.xpath('//div[@class="tableSection"]/table[position() = 1]/tr').each do |e|
    temp << e.css("td, th").map(&:text).map(&:strip).map {|x| x = change x; x.split(/\n/).map {|z| z.gsub(/.{80}/mi, "\\0\n")}.join("\n")}
end

table = Terminal::Table.new
table.headings = temp.shift
table.rows = temp


puts table

Error:

F:\dropbox\Dropbox\temp>ruby nokogiri.rb
nokogiri.rb:21: invalid multibyte char (UTF-8)
nokogiri.rb:21: invalid multibyte char (UTF-8)
nokogiri.rb:21: syntax error, unexpected $end, expecting ')'
...ary/ms189782.aspx').read.tr("í¡","..."))
...                               ^

F:\dropbox\Dropbox\temp>
like image 720
Just a learner Avatar asked Jan 17 '23 09:01

Just a learner


1 Answers

It probably depends on the encoding of the file you're working with, but try using

"\u2026"

for the single-character 3-dots aka "horizontal ellipsis" (the one you want to replace).

like image 86
theglauber Avatar answered Jan 20 '23 00:01

theglauber