Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'csv'
- require 'pry'
- # sqlite3 AddressBook-v22.abcddb
- # .headers on
- # .mode csv
- # .output ZABCDPOSTALADDRESS.csv
- # select * from ZABCDPOSTALADDRESS;
- # .mode csv
- # .output ZABCDRECORD.csv
- # select * from ZABCDRECORD;
- address_filename = "ZABCDPOSTALADDRESS.csv"
- record_filename = "ZABCDRECORD.csv"
- # binding.pry
- addresses = {}
- CSV.read(address_filename, headers: true).entries.map(&:to_hash).map{|h|
- addresses[h["ZOWNER"]] = {
- city: h["ZCITY"],
- state: h["ZSTATE"],
- street: h["ZSTREET"],
- zip: h["ZZIPCODE"]
- }
- h
- }; nil
- people = []
- CSV.read(record_filename, headers: true).entries.map(&:to_hash).each{|h|
- # binding.pry
- address = addresses[h["Z_PK"]]
- if address
- address = "#{address[:street]}, #{address[:city]} #{address[:state]} #{address[:zip]}"
- end
- people << {
- name: "#{h["ZLASTNAME"]} #{h["ZMIDDLENAME"]} #{h["ZLASTNAME"]}",
- address: address
- }
- }; nil
- class Array
- def to_csv(csv_filename="hash.csv")
- require 'csv'
- CSV.open(csv_filename, "wb") do |csv|
- csv << first.keys # adds the attributes name on the first line
- self.each do |hash|
- csv << hash.values
- end
- end
- end
- end
- people.to_csv('people.csv')
- # binding.pry
Add Comment
Please, Sign In to add comment