New line in Python for loop with sqlite3
Question: I'm trying to create a small function that prints successive lines from a
database I'm working with. What I currently have written does that quite well,
except it's all a blocky mess and "\n" is interpreted literally rather than
actually creating a new line.

    
    
    import sqlite3
    conn = sqlite3.connect('database.db')
    database = conn.cursor()
    
    def textfromDB(text):
        for row in database.execute("SELECT * FROM table1 WHERE column1 = ?", [text]):
            print row
            print "\n"
    


Answer: When you print a row inside a `for` loop for sqlite queries, in fact, you
print a tuple. So the mess with newline characters you are talking about is
the question of tuples representation using `print`. If what you need is to
change sparse format with literally interpreted `\n`'s to normal text, then
you have to convert your tuple to a string. You can achieve that by changing
`print row` by this command:

    
    
    print(', '.join(map(str, row))) 
    

What it's doing is joining all elements of a tuple into one string. `map`
function ensures that non-string items (like `int`s) of the tuple converted to
string, before `join` function can concatenate them. If there are no non-`str`
items in your tuples, then you can omit `map` function.

Also, as @timgeb pointed out in comments, you may use pretty print module
`pprint`. It doesn't change `\n`s to actual line breaks, but can print tuples
with nice indentation and other formatting features. It may be quite helpful,
when debugging a program that does something with longish tuples, lists, sets
or dicts. In your case, you'd need to import pprint first:

    
    
    from pprint import pprint
    

And then use `pprint(row)` instead of `print row`. See pprint's
[docs](https://docs.python.org/library/pprint.html) for more details.

