search

Home  >  Q&A  >  body text

How can I display a value without using square brackets, commas or parentheses?

I just want to print the value without any brackets, commas or parentheses. I'm using MySQL with python and mysql.connector.

When I run this code, I get "('esrvgf',)". But I just want "esrvg".

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="password",
  database ="mydatabase"
)

cursor = mydb.cursor()


sql = "select nick from users where ipaddress = '192.168.1.4'"

cursor.execute(sql)

myresult = cursor.fetchall()

for x in myresult:
  print(x)

P粉726133917P粉726133917334 days ago493

reply all(2)I'll reply

  • P粉156532706

    P粉1565327062024-01-01 09:22:48

    By using .fetchall( ) According to the documentation, you will not return a single element, even if there is only one element:

    Therefore, please consider using:

    for x in myresult:
      for y in x:
         print(x)

    Or, if you're sure it's a single element:

    for x in myresult:
       print(x[0])

    reply
    0
  • P粉883973481

    P粉8839734812024-01-01 00:18:50

    cursor.fetchall() Returns a list of tuples (see this question), not a string. If you try to print a tuple, Python will add parentheses, if you try to print a list, Python will add parentheses. All you need to do is print the first element using x[0]. like this:

    for x in myresult:
      print(x[0])

    Alternatively, you can pass each element of the tuple as an argument to print() using the * operator. like this:

    for x in myresult:
      print(*x)

    reply
    0
  • Cancelreply