Solved

Escaping Line Returns in mysql query results

Posted on 2011-09-29
3
202 Views
Last Modified: 2012-05-12
I have a python script that is pulling data from a MySQL database.

Here is a snippet that I use to write the results of the query to file:

cursor = conn.cursor ()
cursor.execute(queryString)
rows = cursor.fetchall ()
for row in rows:
   for col in row:
      output.write("%s\t" % (col))
   output.write("\n")
output.close()

Open in new window


My problem is that multiple fields in the query results are free form text and actually include line returns. Put another way there are multiple fields in the database table being queried that contain free form text including multiple lines and the line returns that come with them.

I need some insight on how I can keep those line returns confined to the one field when I write them to my .csv file and not  actually wrapping around to the next line.
0
Comment
Question by:marcusj2
  • 2
3 Comments
 
LVL 15

Expert Comment

by:mish33
ID: 36892806
One (easy) way is to replace %s with %r which will give you all special character escaped.

Another way to replace(r"\n", "\\n").

Third way to enclose %s in quotes, as most csv dialects allows multi -line strings in quotes.
0
 

Accepted Solution

by:
marcusj2 earned 0 total points
ID: 36934706
Apparently line returns was not my problem!!

There were double quotes in the data returned from mysql that were altering grouping of the data. All I need to do was escape those double quotes...

I ended up making this simple change:
for row in rows:
   for col in row:
      output.write("\"%s\"\t" % (col))
   output.write("\n")
output.close()
cursor.close ()
conn.close ()

Works perfectly.
Thank you though.
0
 

Author Closing Comment

by:marcusj2
ID: 36954327
Feels weird answering my own question.... ha
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now