Currently, i have set the delimiter as comma. I feteching the delimiter from the table and can you let me know how to pass it while reading the file.
def details(conn, ctry, city, code):
cur = conn.cursor()
cur.execute("""select c.delim from function_table c
where c.ctry = %s
and c.city = %s
and c.code = %s """, (ctry, city, code))
row = cur.fetchone()
return row
delimiter8 = details(conn, ctry, city, code)
for file in filenames:
csvFile = pandas.read_csv(file.strip(), sep=‘,’, nrows=1)
I am getting error while passing the delimiter8 variable.
for file in filenames:
csvFile = pandas.read_csv(file.strip(), sep=delimiter8, nrows=1)
When you use .fetchone to get a row from an SQL database, you will get a tuple with every column from the result table - even if there is only one column. (As we like to say, “special cases aren’t special enough to break the rules.”) In this case, you requested a table with one column (select c.delim), and the row that you fetched has ',' for the value in that column, stored in a 1-element tuple (',',). (The trailing comma is how Python tells you that this is a tuple, instead of the () being normal order-of-operations parentheses.)
A tuple is just like a list; you can index into it with numbers starting from 0 onward. Since there is just one element and that’s the one we want to get, that is easy: delimiter8[0].