如何改变…在 Ruby 中(省略号)到 ...(三个句点)?
我正在使用 nokogiri
解析此文档 。我发现该页面中有一些 ...
(省略号)字符并且无法删除。我想知道如何使用 Ruby 将所有 ...
(省略号)替换为 ...
(三个句点)。
顺便说一句,您可以搜索此字符串来查找所有 ...s
指定是否更改表
编辑: 我添加了我的程序和错误消息。
# 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
错误:
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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它可能取决于您正在使用的文件的编码,但尝试使用
单字符 3 点,又名“水平省略号"(您要替换的)。
It probably depends on the encoding of the file you're working with, but try using
for the single-character 3-dots aka "horizontal ellipsis" (the one you want to replace).
我只是先 String#tr 。
然后运行 Nokogiri ......
I'd just String#tr on it first.
And run Nokogiri on that...