Python Forum

Full Version: Line of file from string to list to tuple
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How do I convert a line from a file into a tuple so I can input into mysql database?
File:
Output:
['sql statement %s where id = %s', ['test','1']]
I am struggling with tuple() and list() from a string value...
I am trying to use
Output:
conn.execute(tuple[0],tuple[1])
I think you may want to do some additional parsing, but as is:
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> the_string = "['sql statement %s where id = %s', ['test','1']]"
>>> as_tuples = tuple(the_string.split())
but I doubt that is exactly what you want.
perhaps you could show what you would like the tuples to contain.
After reading another thread and doing some testing, I think I want:
import ast
sqlback = ast.literal_eval(line)
Tuples should be:
sqlback[0] = 'sql statement %s where id = %s'
sqlback[1] = ['test','1']
sqlback[1][0] = 'test'