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 8054047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:03:26+00:00 2026-06-05T08:03:26+00:00

So I’m looping through multiple large xml files and generating MySQL insert statements to

  • 0

So I’m looping through multiple large xml files and generating MySQL insert statements to add rental property listings to a database. Problem is, a number of elements contain special characters like Å or ç or even some dashes and bullets.

I can get the elements just fine, and I can make a string to hold the insert statement, but as soon as I try to execute the statement I get dumped out to the next file.

I’ve got the insert in its own try block, thinking that would just allow me to move on to the next listing rather than scrap the remainder of the xml document, but that’s not happening.

I’ve tried making sure the insert is utf-8 encoded, but it’s not making a difference.

Here is the gist of the code I’ve got:

try:
    print "About to read file: "+fullpath
    data = f.read()  #read the file into a string
    print "Data read from file, now closing: "+fullpath
    f.close()  #close the file, we don't need it any more
    dom = minidom.parseString(data)  #parse the xml
    #get the first child node -- <property_data>
    property_data = dom.firstChild
    properties = property_data.getElementsByTagName('property')
    for property in properties:
        try:
            print "getting details"
            details = property.getElementsByTagName('property_details')
            for detail in details:
                print "attempting to get detail values"
                try:
                     checkin = getElementValue('check_in', detail)
                     name = stripCDATA(getElementValue('name', detail))
                     checkout = getElementValue('check_out', detail)

                                ...etc, etc...

                      print "building insert string"
                      sql = u"""insert into PROPERTY(NAME, CHECKIN, CHECKOUT, etc...)
                                  values(%s,%s,%s,...)""".encode('utf-8')
                      print "starting insert with query:"
                      print sql % (name,checkin,checkout, etc...)
                      try: #HERE IS WHERE THE PROBLEM HAPPENS
                          cursor.execute(sql,(name, checkin, checkout, ...))
                          #display number of rows affected
                          print "Number of rows inserted: %d" % cursor.rowcount
                          conn.commit()
                      except Exception as (errno, strerror):
                          print "Problem inserting the property. Error({0}): {1}".format(errno, strerror)
                except Exception as (errno, strerror):
                    print "Problem with reading/inserting details. Error({0}): {1}".format(errno, strerror)
        except Exception as (errno, strerror):
            print "The loop broke with the following error({0}): {1}".format(errno, strerror)
            errCount += 1
            print "This has happened %d times" % (errCount)
except: #HERE IS WHERE I GET DUMPED TO
    print "Something bad happened while reading and inserting"

As you can see, I’ve got lines printing at various points so I can see when things blow up.

I know it’s parsing the file correctly, I know it’s getting all my elements correctly, I know it’s building the insert statement correctly, and as long as I hit a property with no special characters anywhere in any of the elements I grab, I know it’s inserting into the database correctly. It’s just as soon as it hits a special character that it breaks, and when it breaks it dumps me out 3 levels higher than it should. Yelling and pulling my hair out have been ineffective so far.

Any ideas?

As per the suggestion from @deadly I removed all the try…except blocks and got the following traceback:

Traceback (most recent call last):

File “dbinsert2.py”, line 118, in
cursor.execute(sql,([bunch of var names]))

File “/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py”, line 159, in execute
query = query % db.literal(args)

File “/usr/lib/python2.7/dist-packages/MySQLdb/connections.py”, line 264, in literal
return self.escape(o, self.encoders)

File “/usr/lib/python2.7/dist-packages/MySQLdb/connections.py”, line 202, in unicode_literal
return db.literal(u.encode(unicode_literal.charset))

UnicodeEncodeError: ‘latin-1′ codec can’t encode character u’\u2013’ in position 20: ordinal not in range(256)

  • 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-05T08:03:28+00:00Added an answer on June 5, 2026 at 8:03 am

    Not many people are going to have the patience to work through all that code.

    Start by getting rid of every try…except. Python will still happily raise exceptions without it.

    You only need to use try…except if you want to do some special handling on an exception beyond bug finding. Print statements are better friends at this stage. Also, if you leave off the try…excepts (at least the way you’re using them), Python will also print the traceback which is what you should also post along with your code.

    Please post this traceback once you’ve tidied up your code.

    EDIT: Thanks for the traceback. Now we can see that there’s a mismatch between the encoding you’re using (utf-8) and the default encoding that the MySQLdb Python library uses (latin-1). You need to pass charset='utf8' as a parameter to connect(). (No dash in ‘utf8’ as this is how MySQL stores its list of character sets.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put

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.