Python list format by mysqldb

I am trying to use a class (adns-python) that expects a list in the format:

domain_names = ["google.com", "yahoo.com"] 

This works when I declare a list this way manually. However, I am trying to use the list returned from mysql using python-mysqldb.

When I look at what is returned from mysql using:

 type(mysql_rows) 

This also appears as a list, but when viewing the result:

 print(mysql_rows) 

I see that the list has the format:

  [('google.com',), ('yahoo.com',)] 

I tried listing the output again using a list (mysql_rows) that didn't work. I tried to parse the text manually so that it looked like a list using:

 text_rows = "[" + ", ".join'"%s"' % i for i in mysql_rows = "]" 

which is then displayed as the correct format, but it is a string, not a list, so this also does not work.

These are my first few days studying python, so I'm sorry if this is an obvious / stupid question.

thanks

+6
source share
2 answers

mysql returns a list of tuples. Each tuple is a result string in your result set. If you want to list only the first “column” as a result, try the following:

 first_column = [x[0] for x in mysql_rows] 
+4
source

A list is a list of tuples. Plain

 lst = [x for x, in mysql_rows] 

should be enough.

+6
source

Source: https://habr.com/ru/post/907891/


All Articles