How to Write to csv file to create bulk address book

Discussion in 'Python' started by k.i.n.g., Dec 7, 2008.

  1. k.i.n.g.

    k.i.n.g. Guest

    Hi ,

    I am new to scripting, I am working on script which would create 'n'
    number address book entries into a csv file which would be used to
    import into a address book. I need suggestions for the same

    The fileds for csv file are as follows

    ""Title","First Name","Middle Name","Last
    Name","Suffix","Company","Department","Job Title","Business
    Street","Business Street 2","Business Street 3","Business
    City","Business State","Business Postal Code","Business Country","Home
    Street","Home Street 2","Home Street 3","Home City","Home State","Home
    Postal Code","Home Country","Other Street","Other Street 2","Other
    Street 3","Other City","Other State","Other Postal Code","Other
    Country","Assistant's Phone","Business Fax","Business Phone","Business
    Phone 2","Callback","Car Phone","Company Main Phone","Home Fax","Home
    Phone","Home Phone 2","ISDN","Mobile Phone","Other Fax","Other
    Phone","Pager","Primary Phone","Radio Phone","TTY/TDD
    Phone","Telex","Account","Anniversary","Assistant's Name","Billing
    Information","Birthday","Business Address PO
    Box","Categories","Children","Directory Server","E-mail Address","E-
    mail Type","E-mail Display Name","E-mail 2 Address","E-mail 2 Type","E-
    mail 2 Display Name","E-mail 3 Address","E-mail 3 Type","E-mail 3
    Display Name","Gender","Government ID Number","Hobby","Home Address PO
    Box","Initials","Internet Free
    Busy","Keywords","Language","Location","Manager's
    Name","Mileage","Notes","Office Location","Organizational ID
    Number","Other Address PO
    Box","Priority","Private","Profession","Referred
    By","Sensitivity","Spouse","User 1","User 2","User 3","User 4","Web
    Page""

    All the entries written by csv file by script can be random & dummy as
    this address book is used for testing purpose.


    Thank you in advance,
    Kanthi
     
    k.i.n.g., Dec 7, 2008
    #1
    1. Advertising

  2. k.i.n.g.

    Laszlo Nagy Guest


    > I am new to scripting, I am working on script which would create 'n'
    > number address book entries into a csv file which would be used to
    > import into a address book. I need suggestions for the same
    >

    Please check out the 'csv' module. It comes with Python. Batteries
    included. :)

    http://docs.python.org/library/csv.html

    There are nice examples at the end of that page.
     
    Laszlo Nagy, Dec 7, 2008
    #2
    1. Advertising

  3. k.i.n.g.

    r0g Guest

    k.i.n.g. wrote:
    > Hi ,
    >
    > I am new to scripting, I am working on script which would create 'n'
    > number address book entries into a csv file which would be used to
    > import into a address book. I need suggestions for the same
    >
    > The fileds for csv file are as follows
    >
    > ""Title","First Name","Middle Name","Last
    > Name","Suffix","Company","Department","Job Title","Business
    > Street","Business Street 2","Business Street 3","Business
    > City","Business State","Business Postal Code","Business Country","Home
    > Street","Home Street 2","Home Street 3","Home City","Home State","Home
    > Postal Code","Home Country","Other Street","Other Street 2","Other
    > Street 3","Other City","Other State","Other Postal Code","Other
    > Country","Assistant's Phone","Business Fax","Business Phone","Business
    > Phone 2","Callback","Car Phone","Company Main Phone","Home Fax","Home
    > Phone","Home Phone 2","ISDN","Mobile Phone","Other Fax","Other
    > Phone","Pager","Primary Phone","Radio Phone","TTY/TDD
    > Phone","Telex","Account","Anniversary","Assistant's Name","Billing
    > Information","Birthday","Business Address PO
    > Box","Categories","Children","Directory Server","E-mail Address","E-
    > mail Type","E-mail Display Name","E-mail 2 Address","E-mail 2 Type","E-
    > mail 2 Display Name","E-mail 3 Address","E-mail 3 Type","E-mail 3
    > Display Name","Gender","Government ID Number","Hobby","Home Address PO
    > Box","Initials","Internet Free
    > Busy","Keywords","Language","Location","Manager's
    > Name","Mileage","Notes","Office Location","Organizational ID
    > Number","Other Address PO
    > Box","Priority","Private","Profession","Referred
    > By","Sensitivity","Spouse","User 1","User 2","User 3","User 4","Web
    > Page""
    >
    > All the entries written by csv file by script can be random & dummy as
    > this address book is used for testing purpose.
    >
    >
    > Thank you in advance,
    > Kanthi



    Hi Kanthi, the simple case is to make yourself a list of the fields. For
    each field specify what type of data to generate and a lower and upper
    bound for it's length/value.

    fields = [ ("Title", "txt", 0, 4),
    ("First", "txt", 0, 40),
    ("Tel", num, 8, 15)
    etc...]

    Then repeatedly loop though that list generating random values of the
    appropriate type and appending them to another list to build records.

    import random

    new_record = []
    for each_field in fields:

    if each_field[1] == "txt":
    r = random.randint( each_field[2], each_field[3] )
    new_record.append( r )

    elif each_field[1] == "num":
    r = random.randint( each_field[2], each_field[3] )
    s = "".join( [random.choice("qwerty...etc.") for i in range( r )] )
    new_record.append( s )

    print new_record()

    Then basically make a big list of these records and squirt them out to
    file. You might need read up on the random, csv and copy modules and
    you'll probably want to expand on the data types it can generate.

    Also I use the following class to choose random words from a specified
    word list...

    class WordList:
    def __init__(self, dictionary_filename):
    self.dictionary_list = []
    f = open(dictionary_filename,'r')
    for each in f:
    #print each
    self.dictionary_list.append(each.rstrip())
    f.close()
    self.dictionary_length = len(self.dictionary_list)

    def random(self,x):
    outlist = []
    for each in range(0,x):
    outlist.append(self.dictionary_list[random.randrange(0,
    self.dictionary_length-1)])
    return ' '.join(outlist)


    This would allow you to say:

    firstnames = Wordlist('big_list_of_first_names.txt')

    name = firstnames.random(1)
     
    r0g, Dec 8, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tintin92
    Replies:
    1
    Views:
    1,716
    Andrew Thompson
    Feb 14, 2007
  2. Sonu
    Replies:
    1
    Views:
    808
  3. jliu66
    Replies:
    0
    Views:
    516
    jliu66
    Oct 19, 2007
  4. iPerfect
    Replies:
    2
    Views:
    103
    Brian Candler
    Dec 7, 2008
  5. Sacha Rook

    csv read clean up and write out to csv

    Sacha Rook, Nov 2, 2012, in forum: Python
    Replies:
    2
    Views:
    221
    Hans Mulder
    Nov 2, 2012
Loading...

Share This Page