加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在Ruby中改变……(elipses)到……(三个时期)?

发布时间:2020-12-17 03:46:05 所属栏目:百科 来源:网络整理
导读:我正在使用nokogiri解析 this document.我发现该页面中有一些……(elipses)字符,无法删除.我想知道如何使用Ruby将所有…(elipses)替换为……(三个句点). 顺便说一下,你可以搜索这个字符串来查找所有… s Specifies whether ALTER TABLE 编辑: 我添加了我的
我正在使用nokogiri解析 this document.我发现该页面中有一些……(elipses)字符,无法删除.我想知道如何使用Ruby将所有…(elipses)替换为……(三个句点).

顺便说一下,你可以搜索这个字符串来查找所有… s

Specifies whether ALTER TABLE

编辑:
我添加了我的程序和错误消息.

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

def change s
    {Nokogiri::HTML("&nbsp;").text => " ",Nokogiri::HTML("&quot;").text => '"',Nokogiri::HTML("&trade;").text => '(TM)',Nokogiri::HTML("&amp;").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,"n")}.join("n")}
end

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


puts table

错误:

F:dropboxDropboxtemp>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:dropboxDropboxtemp>

解决方法

它可能取决于您正在使用的文件的编码,但请尝试使用

"u2026"

对于单字符3点又名“horizontal ellipsis”(你要替换的那个).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读