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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:55:23+00:00 2026-06-02T12:55:23+00:00

I am currently working on formatting data between CSV files and an mySQL database.

  • 0

I am currently working on formatting data between CSV files and an mySQL database. I am using the MySQLdb library to manage the connection, but it seems to be some problems with formatting. I have to admit that I’m not a very experienced in neither mySQL or Python, but with a pragmatic approach most have been working out great until now.

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

import MySQLdb 

QUERY = "SELECT * FROM searches WHERE searchdate BETWEEN '2011-08-08' AND '2011-08-14';"
conn = MySQLdb.connect (unix_socket = '/opt/local/var/run/mysql5/mysqld.sock',host =      "localhost", user = "username", passwd= "passwd", db="db")
c = conn.cursor()
c.execute(QUERY)
for row in c.fetchall():
    print row

This is the script which extracts the records from the database. Later in the process I want to extract the data from each of the line and format this into a CSV, but for the moment my problem is that the data printed to screen looks like this:

('\xc3\xa6nima', ' 1', ' 12782027', ' 35', datetime.date(2011, 8, 13))
('\xc3\xa6nima', ' 1', ' 12823616', ' 59', datetime.date(2011, 8, 10))
('\xc3\xa6oc', ' 1', ' 13078573', ' 55', datetime.date(2011, 8, 14))
('\xc3\xa6re', ' 1', ' 12516300', ' 35', datetime.date(2011, 8, 8))
('\xc3\xa6re v\xc3\xa6re deg', ' 1', ' 13145801', ' 59', datetime.date(2011, 8, 13))
('\xc3\xa6re v\xc3\xa6re deg og lammet', ' 1', ' 13145801', ' 59', datetime.date(2011, 8, 13))
('\xc3\xa6re v\xc3\xa6re jesu navn', ' 1', ' 13136667', ' 59', datetime.date(2011, 8, 11))
('\xc3\xa6rlig vuggevise', ' 1', ' 12386933', ' 35', datetime.date(2011, 8, 12))
('\xc3\xa6ror aleina', ' 1', ' 12867037', ' 35', datetime.date(2011, 8, 12))
('\xc3\xa6sj', ' 1', ' 13130891', ' 59', datetime.date(2011, 8, 8))
('\xc3\xa6thenor', ' 1', ' 12555673', ' 35', datetime.date(2011, 8, 10))

What I’m now having problems to understand is how I should get the data in a compatible format. So I guess I want to know how I can access and alter the charset in the database to UTF-8, and whether I need to rebuild all the data or if there is an automatic way of dealing with this issue. I would also be greatfull if anyone could point me in a direction of how I could format the datatime.date with a built-in function (I know I could regex and rebuild, but there is probably a more elegant solution).

In advance thank you for your help!

  • 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-02T12:55:27+00:00Added an answer on June 2, 2026 at 12:55 pm

    In your first column, some of the characters are not printable, so it is converted into hex chars. The last column in a datetime object. Python provides strftime function to convert it into string.

    for row in c.fetchall():
        print row[0], row[1], row[2], row[3], row[4].strftime('%Y-%m-%d')
    

    will work.

    Also, you can write to a file using

    file.write(",".join((row[0], row[1], row[2], row[3], row[4].strftime('%Y-%m-%d'))))
    

    where, file is file object. It will write as comma separated column. Here you can see the original characters in file when you open it.

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

Sidebar

Related Questions

I am currently working on a C# project and I am formatting the data
The database I am working with currently does have two tables, one holding the
I am currently working on a control system for Arduino type devices using Twisted,and
I have a working android app using TextView, some formatting (line breaks, rows of
Currently working on a script to ping every host on a /24 subnet, and
currently working on generation elliptic curve for ECDSA and met some problems: An elliptic
Currently working in the deployment of an OFBiz based ERP, we've come to the
Currently working on a VBScript to automate some of the dirty PST ingestion work
Currently working on a site built in Django and i'm getting an issue when
Currently working with NSURLConnection. Found a great website showing important delegate methods coming with

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.