I have a tab-delimited txt file with rows separated by tabs, and rows separated by newlines. This is what it actually looks like:
476502291\t\tLF3139812164\t\tTitle 1\tKids & Family\nGRAV_2011\t\tThe Full Picture\tIndependent\n [...etc...]
Note that sometimes values are separated by two tabs instead of one.
I need to insert this into a mysql table, which should result in the following:
ID title genre
476502291 Title 1 Kids & Family
GRAV_2011 The Full Picture Independent
How would I read a tab-separated txt file and run a for loop in order to insert values into a table named vendor using MySQLdb?
>>> import MySQLdb
>>> conn = MySQLdb.connect (host = "localhost",
user = "me",
passwd = "password",
db = "my-db")
>>> cursor = conn.cursor ()
>>> # for loop # how to read from the txt file to insert it as required?
>>> # cursor.execute (INSERT...)
>>> conn.commit()
>>> conn.close()
Step 1. Read the
csvmodule. http://docs.python.org/library/csv.html. This does what you want.Step 2. Read up on context managers, also.
This will assure that cursors and files are properly closed.