2

From conditionId input field i am getting comma separated string eg 'Test1','Test2','Test3' below is the code for that i want to split that string and access individual value using for loop

ConditionId =str(request.POST.getlist("ConditionId"))
ConditionIdArray = [n for n in ConditionId[1:-1].split(',')]

for i in range(0,len(ConditionIdArray)):
    print(ConditionIdArray[i])

In print statement it giving me output as

'Test1'
'Test2'
'Test3'

but i want output as

Test1
Test2
Test3

Because when i am storing data in database it stored as 'Test1' not as normal Test1

2 Answers 2

4

POST.getlist will return a list of strings. Don't convert it string and split, just iterate it (no need to use range and indexing):

condition_ids = request.POST.getlist("ConditionId")

for condition_id in condition_ids:
    print(condition_id)
Sign up to request clarification or add additional context in comments.

Comments

2

A little explanatory addition to @falsetru's answer. The str() of a list uses repr() for displaying its elements, thus for [x, y, z] it is:

[repr(x), repr(y), repr(z)]

and not, as one might expect

[str(x), str(y), str(z)]

And for strings, that makes a difference:

> s = 'Test1'
> print(str(s))
Test1

> print(repr(s))
'Test1'  # so, splitting the str(list) on commas leaves the quotes

Comments

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.