i newbie in python need script this: there tables in db; mytable1, mytable2,.. etc;
i want write rows in table file each 1 of tables, exmple:
mytable1: col1 col2 col3 col4 val1 val2 val3 val4 mytable2: col1 col2 col3 col4 val1 val2 val3 val4 val5 val6 val7 val8
my goal; when run script create files table names , write rows comma separated, each row 1 line.
output: mytable1.txt val1,val2,val3,val4 mytable2.txt val1,val2,val3,val4 val5,val6,val7,val8
what efficient way achieve ?
import mysqldb conn = mysqldb.connect(host="..", user="..", passwd="..", db="..") cursor = conn.cursor(mysqldb.cursors.dictcursor) cursor.execute("select * ...") result_set = cursor.fetchall() field_names = [val[0] val in cursor.description] open('mytable1.txt', 'a') f: row in result_set: line = ','.join(row[field_name] field_name in field_names) f.write(line)
you can try this, if using mysql.
cursor = conn.cursor(mysqldb.cursors.dictcursor) cursor.execute("select * your_table") result_set = cursor.fetchall() field_names = [val[0] val in cursor.description] open('mytable1.txt', 'a') f: row in result_set: line = ','.join(row[field_name] field_name in field_names) f.write(line)
Comments
Post a Comment