Skip to content

Instantly share code, notes, and snippets.

@jshiell
Last active January 27, 2016 18:46
Show Gist options
  • Select an option

  • Save jshiell/70490674326cb97b44e8 to your computer and use it in GitHub Desktop.

Select an option

Save jshiell/70490674326cb97b44e8 to your computer and use it in GitHub Desktop.
Turn Santander UK 'xls' export files (actually HTML) into CSV
#!/usr/bin/env ruby
def to_csv(file)
lines = []
file_content = File.open(file, 'r', :encoding => 'iso-8859-1') {|f| f.read}.gsub(/\n/, '')
file_content.scan(/<tr.*?>(.+?)<\/tr>/) do |match|
next unless match[0][/\d{2}\/\d{2}\/\d{4}/] and not match[0][/XXXX/]
fields = []
match[0].scan(/>([^<>]+?)</) do |field|
fields.push(field[0].strip.encode('utf-8').gsub(/[£, ]/, ''))
end
lines.push(fields.join(','))
end
lines.join("\n")
end
def print_usage_and_exit()
puts "Usage: #{__FILE__} <filename of rubbish Santander 'xls' file aka HTML>"
exit 1
end
if __FILE__ == $0
print_usage_and_exit if ARGV.empty?
puts to_csv(ARGV[0])
end
@jshiell

jshiell commented Jan 27, 2016

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment