0

I have a database like this: enter image description here

When i retrieved this column and want to convert it to Json format like this:

cursor3 = conn.cursor()
cursor3.execute("select version()")
data = cursor3.fetchone()
cursor3.execute("SELECT headerdynamic FROM emailData ")
headerDynamic = cursor3.fetchall()
di = {i.split(':')[0]:i.split(':')[1] for i in headerDynamic}
js = json.dumps(di)
print(js)

It will prompt this error message:

di = {i.split(':')[0]:i.split(':')[1] for i in headerDynamic}
AttributeError: 'tuple' object has no attribute 'split'

Here is the print output of the headerDynamic

 [("'header1 : Subject', 'header2 :Text'",)] 

Does anyone know what im doing wrong here? Thanks

1

2 Answers 2

1

Use an extend method to convert your tuple to an array

x = []
for item in a:
    x.extend(item)
Sign up to request clarification or add additional context in comments.

Comments

0

You can print out headerDynamic and see what kind of data is inside. Most likely, you will find tuple datatypes which cannot be splitted the way you try.

Iterate over your tuples (Not your Array which contains tuples!):

for i in ("'header1 : Subject', 'header2 :Text'",):
    # i is a string, which has your intended split function
    print(i)

2 Comments

Here is the print output [("'header1 : Subject', 'header2 :Text'",), ("'header1 : Subject', 'header2 :Text'",)] and the type is list
Yes but inside the list, there are tuples; identified by the (,) syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.