Guest User

Untitled

a guest
Dec 15th, 2018
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. require 'csv'
  2. require 'pry'
  3.  
  4. # sqlite3 AddressBook-v22.abcddb
  5. # .headers on
  6. # .mode csv
  7. # .output ZABCDPOSTALADDRESS.csv
  8. # select * from ZABCDPOSTALADDRESS;
  9. # .mode csv
  10. # .output ZABCDRECORD.csv
  11. # select * from ZABCDRECORD;
  12.  
  13. address_filename = "ZABCDPOSTALADDRESS.csv"
  14. record_filename = "ZABCDRECORD.csv"
  15.  
  16. # binding.pry
  17.  
  18. addresses = {}
  19.  
  20. CSV.read(address_filename, headers: true).entries.map(&:to_hash).map{|h|
  21. addresses[h["ZOWNER"]] = {
  22. city: h["ZCITY"],
  23. state: h["ZSTATE"],
  24. street: h["ZSTREET"],
  25. zip: h["ZZIPCODE"]
  26. }
  27.  
  28. h
  29. }; nil
  30.  
  31. people = []
  32.  
  33. CSV.read(record_filename, headers: true).entries.map(&:to_hash).each{|h|
  34. # binding.pry
  35. address = addresses[h["Z_PK"]]
  36.  
  37. if address
  38. address = "#{address[:street]}, #{address[:city]} #{address[:state]} #{address[:zip]}"
  39. end
  40.  
  41. people << {
  42. name: "#{h["ZLASTNAME"]} #{h["ZMIDDLENAME"]} #{h["ZLASTNAME"]}",
  43. address: address
  44. }
  45. }; nil
  46.  
  47. class Array
  48. def to_csv(csv_filename="hash.csv")
  49. require 'csv'
  50. CSV.open(csv_filename, "wb") do |csv|
  51. csv << first.keys # adds the attributes name on the first line
  52. self.each do |hash|
  53. csv << hash.values
  54. end
  55. end
  56. end
  57. end
  58.  
  59. people.to_csv('people.csv')
  60.  
  61.  
  62. # binding.pry
Add Comment
Please, Sign In to add comment