1

I am working with python plugins.I have my field variable = "t_0"."survey" I wanted to store only survey into another variable.Which function to use to get survey from "t_0"."survey"?

I tried a=field.split(".") when i try to print a ,it gives

<PyQt4.QtCore.QStringList object at 0x01247228>

Is there any delete function or to find position of "." from the string..?

If i try lstrip() or ljust() ,it gives error saying

AttributeError: 'QString' object has no attribute 'lstrip'..
1
  • 1
    Possibly just try a=str(field) b=a.split(".") Commented Feb 15, 2012 at 10:01

3 Answers 3

3

If a is a QString, then calling a.split produces a QStringList, just as calling split on a Python str produces a list:

>>> qstr = QString("t_0.survey")
>>> slist = qstr.split(".")
>>> slist
<PyQt4.QtCore.QStringList object at 0x00BBCD88>

You can either cast QStringList to a Python list:

>>> list(slist)
[PyQt4.QtCore.QString(u't_0'), PyQt4.QtCore.QString(u'survey')]

or just extract the second element:

>>> slist[1]
PyQt4.QtCore.QString(u'survey')

And perhaps get rid of the QString wrapping:

>>> unicode(slist[1])
u'survey'
Sign up to request clarification or add additional context in comments.

Comments

1

If your field variable is of type QString the below code works fine for me.

 QString str = "a,b,c";    
 QStringList list1 = str.split(",");

Output = [ "a","b", "c" ]

Try doing type(field) in your python interpreter and show us the output.

OR type cast your variable to str like this a=str(field).split(".")

Comments

0

It seems like 'a' is not really a String, but some other object generated by the PyQt library (maybe an input field ?). If you can find a way to get a real String out of this field (a.value(), a.text(), or something like that), you should be able to use split.

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.