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("<").text => "<",
Nokogiri::HTML(">").text => ">",
Nokogiri::HTML("©").text => "(C)",
Nokogiri::HTML("®").text => "(R)",
Nokogiri::HTML("¥").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>
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With