Formatting addresses question

M

Matthew Margolis

I currently have a large text file based address book(for myself, not
spamming) that I am working on making suitable for import into a
database. To do this I need comma separated values. How would I go
about adding a comma after the state in the person's mailing address?
An example record:

Name: Bob Fakeyhead
Title: Old Boss
Phone: 555-555-5555
Mailing: 23456 Something Street
OldPlace, WA 55555

I need a ',' after WA.
Right now I am reading the file into a string to edit it and then
writing the comma separated version back out to file. For most of the
other fields I can just add a comma to the end of each line and get the
desired effect but for the Address I need to have multiple fields and
therefore multiple commas per line.

Thank you,
Matthew Margolis
 
D

Daniel Kelley

Matthew Margolis said:
I currently have a large text file based address book(for myself, not
spamming) that I am working on making suitable for import into a
database. To do this I need comma separated values. How would I go
about adding a comma after the state in the person's mailing address?

An example record:

Name: Bob Fakeyhead
Title: Old Boss
Phone: 555-555-5555
Mailing: 23456 Something Street
OldPlace, WA 55555


If you are using Ruby 1.8, you can use the CSV module.

http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/



::::::::::::::
z.rb
::::::::::::::
#
# z.rb
#
# Copyright (c) 2004 by Daniel Kelley
#
#

require 'csv'

writer = CSV.generate('csvfile.csv')
writer << ['Name', 'Title', 'Phone', 'Mailing']
writer << ['Bob Fakeyhead', 'Old Boss', '555-555-5555', '23456 Something Street
OldPlace, WA 55555']
writer.close

::::::::::::::
csvfile.csv
::::::::::::::
Name,Title,Phone,Mailing
Bob Fakeyhead,Old Boss,555-555-5555,"23456 Something Street OldPlace, WA 55555"
 
B

Brian Schroeder

Matthew Margolis said:
I currently have a large text file based address book(for myself, not
spamming) that I am working on making suitable for import into a
database. To do this I need comma separated values. How would I go
about adding a comma after the state in the person's mailing address?

An example record:

Name: Bob Fakeyhead
Title: Old Boss
Phone: 555-555-5555
Mailing: 23456 Something Street
OldPlace, WA 55555

If you are using Ruby 1.8, you can use the CSV module.

http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/

[...]
require 'csv'


If I understand him correctly, this solves creating correct csv output,
but he has to parse the input.

A regexp to split the adress line would be
raise "Unknown address format" unless /(.*?),\s*(\w*)\s*(\d*)/ =~ address_line
address[:town] = $1; address[state] = $2, address[:code] => $3}

regards,

Brian
 
M

Matthew Margolis

Daniel said:
I currently have a large text file based address book(for myself, not
spamming) that I am working on making suitable for import into a
database. To do this I need comma separated values. How would I go
about adding a comma after the state in the person's mailing address?

An example record:

Name: Bob Fakeyhead
Title: Old Boss
Phone: 555-555-5555
Mailing: 23456 Something Street
OldPlace, WA 55555


If you are using Ruby 1.8, you can use the CSV module.

http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/



::::::::::::::
z.rb
::::::::::::::
#
# z.rb
#
# Copyright (c) 2004 by Daniel Kelley
#
#

require 'csv'

writer = CSV.generate('csvfile.csv')
writer << ['Name', 'Title', 'Phone', 'Mailing']
writer << ['Bob Fakeyhead', 'Old Boss', '555-555-5555', '23456 Something Street
OldPlace, WA 55555']
writer.close

::::::::::::::
csvfile.csv
::::::::::::::
Name,Title,Phone,Mailing
Bob Fakeyhead,Old Boss,555-555-5555,"23456 Something Street OldPlace, WA 55555"
Thanks for the reply but I already have decided on what database I will
be using so I don't need CSV, I just need to find a way to insert a
comma after the state in the person's address.

-Matthew Margolis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top