-1

how can I delete the extra brackets from result? {{ }}

Code for connect with database:

import pymysql

try:
    db = pymysql.connect(host='localhost', user='root', password='', db='testdb', )
    cursor = db.cursor()
    cursor.execute("SELECT text FROM wiadomosci WHERE msgid=1")
    result = cursor.fetchall()
except:
    result = '[Something went wrong][1] :('

Code for tkinter:

textExample = Text(window, height=1)
textExample.insert(1.0, "Message from owner: ")
textExample.config(state=DISABLED)
textExample.pack()

textExample2 = Text(window, height=3)
textExample2.insert(1.0, result)
textExample2.config(state=DISABLED)
textExample2.pack()

Photo of the problem

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
xzibus
  • 1
  • If the methods in the answers don't work, try printing `result`. If it's a list or a tuple then, join the elements into a string using [this](https://stackoverflow.com/a/493842/11106801) – TheLizzard Jul 06 '21 at 18:50

4 Answers4

0

You don't actually see {curly braces}, do you? I'll wager you're actually seeing square brackets. cursor.fetchall() returns a list of lists. You happen to be getting a list with one record, and that record has one field. Thus, it will render as "[[message]]".

The proper way to handle a single record with a single field is:

result = cursor.fetchone()[0]
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Tkinter (well, tcl) renders lists using curly brackets. Plus, the OP literally shows curly braces, not square braces. – Bryan Oakley Jul 06 '21 at 19:02
0

You are passing a list or tuple to a method that requires a string. In your case, result is a tuple of tuples representing rows and columns. When you do that, tkinter adds curly braces due to how the underlying tcl interpreter handles lists.

The solution is to convert your data to a string explicitly rather than letting tcl do it, or extract a string from the data.

For example, if you want the first column of the first row of result you could do this:

textExample2.insert("1.0", result[0][0])

You could also use fetchone so that it only returns a single row. You still have to pull data out of the first column. This sets result to the string in the first column:

result = cursor.fetchone()[0]
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
-1

This should work

"{{yourText}}".replace("{", "").replace("}", "")
dchoruzy
  • 249
  • 2
  • 8
-1

result[2:-2] is the most robust way to do this, since we know that the message is always between {{ and }}. Don't try to replace brackets directly, or you may modify the message from the database. For example, if the message is "{{the result is {a:2,b:3}}}" the other answers would change this to "the result is a:2,b:3" instead of "the result is {a:2,b:3}" as intended.

However, you might want to investigate why the brackets are there in the first place. Maybe you're stringifying an object rather than extracting the data you want from that object?

  • `result` is not a string, so this solution won’t work. The raw data doesn’t actually have curly braces in it. – Bryan Oakley Jul 06 '21 at 19:34
  • You can always convert the result to a string, since that's what happens implicitly anyway when displaying it: `str(result)[2:-2]`. – public satanic void Jul 06 '21 at 19:43
  • Yes, you can convert the result to a string, but that’s not what this answer says to do. But even if you convert it to a string, it won’t have the curly braces in it. They do not exist in the raw data. – Bryan Oakley Jul 06 '21 at 19:50