Last active
January 27, 2016 18:46
-
-
Save jshiell/70490674326cb97b44e8 to your computer and use it in GitHub Desktop.
Turn Santander UK 'xls' export files (actually HTML) into CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And yes, I know ...