[Fixed]-Django view: Convert unicode retrieved from db to string

0👍

I found the solution.

 import ast
 str_diag_val =ast.literal_eval(diag_val)
 print "diag value=", str_diag_val[0], " ", str_diag_val[1]

And this converts string representation of list to a list.

👤zinon

1👍

This isn’t really a question about Django, it’s about lists and strings in Python.

It looks like diag_val is a list, not unicode. You can concatenate a list of strings with join:

", ".join(diag_value)

0👍

What we want to do here is convert a list, diag_val to a string, say str_diag_val.
We can do this using the ‘join’ method.

 diag_patient = Diagnosis.objects.get(patient=myid)
 diag_val = diag_patient.diagnosis_option
 str_diag_val = ", ".join(str(s) for s in diag_val) //to typecast the elements of the list into string
 print "diagnosis value=", str_diag_val

Leave a comment