Apologies for the mis-understanding, I have been using backslashes.
Here's my code, as you'll see from the resulting file there are a
bunch of new line characters in the file I'd like to get rid of.
Thanks for the help so far.
require("rubygems")
require("scrubyt")
require ("open-uri")
require 'time'
require 'date'
psi = Scrubyt::Extractor.define do
fetch("http://app.nea.gov.sg/psi/")
record("/html/body/div/table/tr/td/table/tbody/tr/td/div",
{ :generalize => true }) do
title("/strong[1]/font[1]")
item("/table/tbody/tr/td/table/tbody/tr", { :generalize => true })
do
region("/td[1]")
psi("/td[7]")
aqd("/td[8]")
end
end
end
f = open("psiregions.xml", File::CREAT|File::TRUNC|File::RDWR) {|f|
psi.to_xml.write(f, 1)
}
# Create the RSS file.
rssfile = File.new("sgpsi.xml", "w")
rssfile.puts('<?xml version="1.0" encoding="UTF-8"?>')
rssfile.puts('<rss version="2.0">')
rssfile.puts(' <channel>')
rssfile.puts(' <link>http://app.nea.gov.sg/psi/</link>')
rssfile.puts(' <description>Singapore PSI Readings</description>')
#rssfile.puts(' <title>Singapore PSI Readings' + Time.now.rfc2822
+ '</title>')
rssfile.puts(' <lastBuildDate>' + Time.now.rfc2822 + '</
lastBuildDate>')
rssfile.puts(' <webMaster>singeo / singeo.com.sg</webMaster>')
File.open('psiregions.xml', 'r') do |f1|
while line = f1.gets
line=line.strip
line=line.chomp
line.gsub!(/[\n]/, "")
line.gsub!(/<root>/, "")
line.gsub!(/<\/root>/, "")
line.gsub!(/<record>/, "")
line.gsub!(/<\/record>/, "")
line.gsub!("24-hr", "Singapore 24-hr")
line.gsub!("<region>Region</region>", "")
line.gsub!("<region>Sulphur Dioxide</region>", "")
line.gsub!(/<region>/, "<title>")
line.gsub!(/<\/region>/,":")
line.gsub!(/<psi>/, " PSI Level ")
line.gsub!(/<\/psi>/, "")
line.gsub!(/<aqd>/, " - ")
line.gsub!(/<\/aqd>/, "</title>")
line.gsub!(/<item>/, "<item><pubDate>" + Time.now.rfc2822 + "</
pubDate>")
rssfile.puts line
end
end
rssfile.puts('</channel>')
rssfile.puts('</rss>')
rssfile.close
On May 17, 4:38 pm, Dan Zwell <dzw... / gmail.com> wrote:
> Singeo wrote:
> > Hi, I'm pretty new to Ruby. I've got a text file where I need to
> > remove some new line characters. I've tried everything I can think of
> > to do this with no success, including:
>
> > line.gsub!("/r","")
> > line.gsub!("/n","")
> > line=line.chomp
>
> > I can't seem to get the new line character to be recognised and dealt
> > with. Any advice appreciated.
>
> > Thanks
>
> It looks like you should be using backslashes. If you want to match both
> newlines and carriage returns, you can use:
>
> line.gsub!(/[\n\r]/, "")
>
> -Dan