Python Query ( string) - python

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'..

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'

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(".")

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.

Related

Python Selenium: Alternative to string formatting for lists?

I am trying to take the value from the input and put it into the browser.find_elements_by_xpath("//div[#class='v1Nh3 kIKUG _bz0w']") function. However, the string formatting surely doesn't work, since it's the list, hence it throws the AttributeError.
Does anyone know any alternatives to use with lists (possibly without iterating over each file)?
xpath_to_links = input('Enter the xpath to links: ')
posts = browser.find_elements_by_xpath("//div[#class='{}']").format(devops)
AttributeError: 'list' object has no attribute 'format'
Looks like the reason of error is that you are placing the format function in the wrong place, so instead of operating on string "//div[#class='{}']" you call it for the list returned by find_elements_by_xpath. Could you please try to replace your code with one of the following lines ?
posts = browser.find_elements_by_xpath("//div[#class='{}']".format(devops))
posts = browser.find_elements_by_xpath(f"//div[#class='{devops}']")

How to access PyNode from a PyNode list?

I ran this example code with 2 polyCubes in scene.
import pymel.core as pymel
pymel.select('pCube1', 'blinn1')
print pymel.ls(sl = True)
print pymel.ls(sl = True)[0]
and this is my output
[nt.Transform(u'pCube1'), nt.Blinn(u'blinn1')]
pCube1
I know the elements inside this list are PyNodes, but printing them gives out a string type name of the node. Is there anyway to access the PyNode directly from this list?
Found the answer myself.
So apparently the Script Editor returns a representation of PyNode when we print it. Like it's an overloaded str. It is still a PyNode but looks like a string only in Maya's Script Editor. To make it actually appear like a PyNode, we have to use repr() or enclose in back-ticks (`)
Here is the link where I found the answer.
: http://download.autodesk.com/us/maya/2011help/pymel/tutorial.html
Formatting: Read Me First to Avoid Confusion section

Error in using slice assignment in strings

website = 'http://www.python.org'
website[18:] = 'com'
The error says:
'str' object does not support item assignment.
Why is this code snippet not legal?
Because strings are immutable. Do it like this:
>>> website = 'http://www.python.org'
>>> website = website[:18] + 'com' # build a new string, reassign variable website
>>> website
'http://www.python.com'
If you prefer not to count how many characters to stop before you reach .org
you can use
website=website[:len(website)-4]+".com"

Getting the value of a key in a dict

I try to get the value of a key in a dict by :
print User['LocationName']
This is giving me a TypeError: string indices must be integers, not str error.
This is the dict
{u'LocationName': u'home', u'First Name': u'Bob',...... }
I'm not sure what this error means
User is not a dict. User is a string. Figure out why it's a string, and change that.
The error means that you are trying to access a "part" of your string as if it was a list. But you are not using a integer as parameter to access a list, you are using a string.
Try to use:
eval(User)
then you can:
print User['LocationName']
This usually happens when you save a JSON into a file, using python. It's like Python saved your dict as a dict Object, but when you read from the file, the object is read by string. So, when you call eval(), you get a dict again.

with This below Code i am getting error AttributeError: 'str' object has no attribute 'items' ,

import json
import simplejson
import urllib2
data = urllib2.urlopen('www.example.com/url/where/i/get/json/data').read()
j = ""
j = simplejson.loads(data)
dump_data=simplejson.dumps(j)
for data in j["facets"]:
print data.items()
print "\n----------------\n"
The error message says it all. Clearly j["facets"] is an iterable which contains at least some strings instead of containing some other datatype which has an items method. (maybe you expected a dict)?
Try printing j["facets"] to see what you're actually getting there. Then you might be able to figure out why you're getting a string instead of the expected object (dict).
Title contains the answer
j["facets"] is probably a list of string items

Categories