This question already has answers here:
Get plain text from QLineEdit
(2 answers)
Closed 7 years ago.
I am using Qt and want to get string from text field. I found below solution but it returns a QString. I just want to avoid writing extra code to extract string further. Can anyone suggest a simple solution to retrieve text from text field.
text = self.your_plugin_dlg.ui.yourLineEdit.text()
Well, you will have to add some code to get a string from a QLineEdit object. Simplest solution: use the str() method.
text = str( self.your_plugin_dlg.ui.yourLineEdit.text() )
for example http://forums.devshed.com/python-programming-11/qstring-normal-python-string-127725.html says:
text = str(self.your_plugin_dlg.ui.yourLineEdit.text())
But i agree, it's a pitty we cant use strings easily in Qt.
other hints with Qt and strings: http://pyqt.sourceforge.net/Docs/PyQt4/gotchas.html
Related
This question already has answers here:
Removing control characters from a string in python
(9 answers)
Closed 2 years ago.
I am getting a text like below by reading a word file
Exe Command\r\x07
My desired text is
Exe Command
I tried this solution but it gives me
Exe Command\r
How can i remove 2 any backslash characters? I would like a speed friendly solution because I have thousands of inputs like this.
You can use replace() method twice.
In [1]: myStr.replace("\r", "").replace("\x07", "")
Out[1]: 'Exe Command'
If this isn't working, you can try using raw string
In [1]: myStr.replace(r"\r", "").replace(r"\x07", "")
Out[1]: 'Exe Command'
EDIT: As per comment, for removing any of those control characters, use this post's solution.
import unicodedata
def remove_control_characters(s):
return "".join(ch for ch in s if unicodedata.category(ch)[0]!="C")
All credits for this solution goes to Alex Quinn.
This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 3 years ago.
I am new to Python and stack overflow. How do I use the count and capitalize functions for my strings? The app I am learning from appears to have this mixed up because it dosen't work in vscode.
It seems like the capitalize function call is ignored. What am I doing wrong? If someone could tell me how to use the count function as well I have the same problem. The app I learn from is called Programminz.
This is Python 3
practice = "CaPITalIzE mE proPerLy"
practice.capitalize()
print(practice)
string_name.capitalize()
string_name: It is the name of string of
whose first character we want
to capitalize.
Try to use :
string = "CaPITalIzE mE proPerLy"
capitalized_string = string.capitalize()
print('Old String: ', string)
print('Capitalized String:', capitalized_string)
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 5 years ago.
I have some problems with using a list in python.
Right now, I open a .txt file with data, and read it into my python file.
However, when I put the input from the datafile into variable data and print this to check if it works or not, I see a lot of extra brackets which I don't want. Right now, it looks like:
["['sports','pizza','other']"]
and I want it to have it in a way like this:
['sports','pizza','other']
Can someone help me to get this work? Reason why I want it in a format like I mentioned above, is that I want to compare the list with another list, and that does not work in the format with the ]"]
I hope someone will help me.
Simply use eval function from Python.
>>> a = ["['sports','pizza','other']"]
>>> eval(a[0])
['sports', 'pizza', 'other']
This question already has answers here:
Get plain text from QLineEdit
(2 answers)
Closed 7 years ago.
I am using PyQt and want to get string from text field. I found below solution but it returns a QString. I just want to avoid writing extra code to extract string further. Can anyone suggest a simple solution to retrieve text from text field.
text = self.your_plugin_dlg.ui.yourLineEdit.text()
Thanks.
The only way to get the text is converting the QString into a String
text = str(self.your_plugin_dlg.ui.yourLineEdit.text())
This question already has an answer here:
Splitting an input in to fragments (Python)
(1 answer)
Closed 9 years ago.
I need to somehow convert a mathematical input(str) to a number,
e.g.
4-3*2-1+5 = ((((4-3)*2)-1)+5).
Current code looks like this:
Answer = input ('Put your answer here: ')
4-3*2-1+5
Somehow, I need to remake the string in to smaller fragments so that it reads from left to right, and to remake the numbers in to integers, but I have no idea how to do it.
I tried doing
Answer.split('+','-','*','/')
But it says TypeError: split() takes at most 2 arguments (4 given)
Also tried adding the answer to a list to see if that helped me at all:
li.append(Answer)
(li = ['4-3*2-1+5']
But I don't see anything beneficial with that..
Please help!
(I'm new to SOF, so if there's any information that's missing, please tell me what and I will try to correct it).
What you need to write is a parser and simple evaulator for simple expressions.
I would start reading any of the following:
http://pyparsing.wikispaces.com/HowToUsePyparsing
http://pyparsing.wikispaces.com/Examples
http://kmkeen.com/funcparserlib/
There are many other parser libraires, but these are just a couple.
You could also just use the rply library which if you have a look at the PyPi page has an example that directly implement and simple expression parser and evaluater just like what you're describing in your question.