0

I have the following code except that gives me the following list of tuples

 if request.method == 'POST':
    checkbox_values = request.form.getlist('service_name')
    print(type(checkbox_values)) //for debugging
    print(checkbox_values) //for debugging

[u"(u'ZOL Zimbabwe', u'Fibre Internet')", u"(u'ZOL Zimbabwe', u'Wimax Internet')", u"(u'Utande Internet Services', u'Fibre Internet')", u"(u'Utande Internet Services', u'Wimax Internet')"]

In simple english , the first value in every tuple is the ISP name and the second value is the service provided by that ISP

What i need to achieve is being able to have a reference of the first value and the second value.

If i do this

values =     [u"(u'ZOL Zimbabwe', u'Fibre Internet')", u"(u'ZOL Zimbabwe', u'Wimax Internet')", u"(u'Utande Internet Services', u'Fibre Internet')", u"(u'Utande Internet Services', u'Wimax Internet')"]

for x in value:
    print k

i get this

(u'ZOL Zimbabwe', u'Fibre Internet')
(u'ZOL Zimbabwe', u'Email Hosting')
(u'Utande Internet Services', u'Fibre Internet')
(u'Utande Internet Services', u'Wimax Internet')
(u'Utande Internet Services', u'Email Hosting')

i need a way of referencing these values so i can grab them and insert into a database

i looked into these links ,but every method is giving me an error

Tuple unpacking in for loops

Iterate a list of tuples

iterating quickly through list of tuples

and I am still reading those links ,in case i am missing something ,someone can point me to the right direction

Community
  • 1
  • 1
Chamambom
  • 127
  • 1
  • 1
  • 10
  • 4
    You don't have tuples. You have *strings with parentheses in them*. They are probably produced *from* tuples, but someone passed them to the `unicode()` function. – Martijn Pieters Nov 18 '16 at 18:01
  • Printing those strings will write out text that *looks* like Python syntax, but they are still strings. – Martijn Pieters Nov 18 '16 at 18:02
  • 2
    Put differently: fix the code that produced those strings, and leave those tuples as tuples. You can undo the damage with `ast.literal_eval()` but you'd be much better off not creating these results in the first place. – Martijn Pieters Nov 18 '16 at 18:02
  • @MartijnPieters the code that is producing that is this `if request.method == 'POST': checkbox_values = request.form.getlist('service_name')` – Chamambom Nov 18 '16 at 18:05
  • So why does the server send you Python literals as strings, and not as, say, JSON? – Martijn Pieters Nov 18 '16 at 18:12

0 Answers0