Solved

Escaping Line Returns in mysql query results

Posted on 2011-09-29
3
204 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

910 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

19 Experts available now in Live!

Get 1:1 Help Now