Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8426543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:33:31+00:00 2026-06-10T04:33:31+00:00

What is the best way to format data? Here is the background : I

  • 0

What is the best way to format data?

Here is the background :

I am using nameparser to parse a name the best way possible. I built a wrapper that calls the nameparser and then stores the parsed name in the database (MySQL).

What would be the most efficient approach in this case? Following is my approach.

  1. Step 1 : call nameparser (provide tablename, id, name, first,
    middle, last, suffix).
  2. Step 2 : Store the parsed (returned) name in
    a dict of this form in memory (I am parsing relatively small names
    set – say 20,000 names). {id:{'first':'John', 'middle':'V',
    'last':'Doe', 'suffix':''}
  3. Step 3 : Store the dict in the MySQL
    table with one query? (Not sure if it is possible with the data
    structure described in Step 2.

Here is my code :

#!/usr/bin/python
# -*- coding: utf-8 -*-

from nameparser import HumanName

import time

cursor = db.cursor()


def name(table, id, name, first, middle, last, suffix):

    cursor.execute('SELECT `' + id + '`,`' + name + '` FROM `' + table
                   + '` WHERE `' + name + '` IS NOT NULL AND ' + id
                   + ' IS NOT NULL')

    numrows = int(cursor.rowcount)
    namelist = []
    namelist = cursor.fetchall()

    for record in namelist:
        parsed = HumanName(record[1])
        parsed.capitalize()
        mydict[int(record[0])] = {
            'first': str(parsed.first),
            'middle': str(parsed.middle),
            'last': str(parsed.last),
            'suffix': str(parsed.suffix),
            }   

mydict = {}

starttime = time.time()

split = name('NamesToParse','id','name','first','middle','last','suffix')
print mydict
print time.time() - starttime

Please suggest the best way of storing the data in the MySQL table. This is what I have so far and I still have to loop through each record. I am wondering if there is a way to update the existing table rather than having to create a temp table first and then updating the original table in one go? Hope I made sense.

for id, val in mydict.items():

    sorted_keys = sorted(map(str, val.keys()))
    sorted_vals = map(encoding, [val[mydict] for mydict in sorted_keys])  # sorted by keys
    formatted = ', '.join(["'%s'"] * len(sorted_vals))
    db.execute("""insert into NamesToParseOut(%s) values (%s)""" % (', '.join(sorted_keys), formatted), sorted_vals)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:33:32+00:00Added an answer on June 10, 2026 at 4:33 am

    Looks like I will take the List of tuples approach and insert in a temp table first followed by updating them with the original table. The time savings are amazing. I feel dictionary is an overkill for this task.

    mylistoftuples.append((int(record[0]), str(parsed.first),str(parsed.middle),str(parsed.last),str(parsed.suffix)))
    
    cursor.executemany("""insert into NamesToParseOut(id, first, middle, last, suffix) values (%s, %s, %s, %s, %s)""", mylistoftuples)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the best way to return a Java Map using the JSON format? My
What's the best way to parse a file in SpreadsheetML format with Perl? I've
What is the best way to convert a string to DateTime? The format of
I am unsure which is the best way to go here. I have a
I’m trying to decide the best way to load data into my app, it’s
Trying to figure out the best way (either by using what I know in
I'm looking for the best way to read data from an stdin pipe in
What is the best way to upload data entered by user in jQuery Handsontable
What is the best way to work with PDF and DJVU formats in Ruby/Ruby
Best way to match 2 pages for Google analytics. Pages to Query: 1.) this_google.html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.