Better code then a regex sub() python 2.7 - regex

I am trying to find out if there are better faster ways to clean this returned string. Or is this the best way. It works, but more efficient ways are always wanted.
I have a function that returns the following output:
"("This is your:, House")"
I clean it up before printing with:
a = re.sub(r'^\(|\)|\,|\'', '', a)
print a
>>> This is your: House
I also learn a lot from the different ways people do things.

You don't need to use regular expression to do this.
>>> import string
>>> a = '"("This is your:, House")"'
>>> ''.join(x for x in a if x not in string.punctuation)
'This is your House'
>>> tbl = string.maketrans('', '')
>>> a.translate(tbl, string.punctuation)
'This is your House'

s='"("This is your:, House")"'
s.replace('\"','').replace('(','').replace(')','').replace(',','').replace(':','')
'This is your House'

Related

From SAS to Python : substr

I would like to use the function substr(my_var,1,2) of SAS in Python. I tried a lot of functions like contains(), split() but it didn't work.
The functions contains() and split() work only for a string value. I would like to use it on a Python Series without using a for.
Thanks a lot for your help
A string in python can be sliced like any list:
>>> str = 'Hello World'
>>> str[1:3]
'el'
>>> str[1:-2]
'ello Wor'
To get substrings for multiple strings, you can use list comprehensions:
>>> strs = ['Hello World', 'Foobar']
>>> [ str[1:4] for str in strs]
['ell', 'oob']
In python, you may try this:
my_var[1:3]
This gets sub string of my_var from position 1 to 3 (exclusive).

Testing for an item in lists - Python 3

As part of a school project we are creating a trouble shooting program. I have come across a problem that I cannot solve:
begin=['physical','Physical','Software','software',]
answer=input()
if answer in begin[2:3]:
print("k")
software()
if answer in begin[0:1]:
print("hmm")
physical()
When I try to input software/Software no output is created. Can anybody see a hole in my code as it is?
In Python, slice end values are exclusive. You are slicing a smaller list than you think you are:
>>> begin=['physical','Physical','Software','software',]
>>> begin[2:3]
['Software']
>>> begin[0:1]
['physical']
Use begin[2:4] and begin[0:2] or even begin[2:] and begin[:2] to get all elements from the 3rd to the end, and from the start until the 2nd (inclusive):
>>> begin[2:]
['Software', 'software']
>>> begin[2:4]
['Software', 'software']
>>> begin[:2]
['physical', 'Physical']
>>> begin[0:2]
['physical', 'Physical']
Better yet, use str.lower() to limit the number of inputs you need to provide:
if answer.lower() == 'software':
With only one string to test, you can now put your functions in a dictionary; this gives you the option to list the various valid answers too:
options = {'software': software, 'physical': physical}
while True:
answer = input('Please enter one of the following options: {}\n'.format(
', '.join(options))
answer = answer.lower()
if answer in options:
options[answer]()
break
else:
print("Sorry, {} is not a valid option, try again".format(answer))
Your list slicing is wrong, Try the following script.
begin=['physical','Physical','Software','software',]
answer=input()
if answer in begin[2:4]:
print("k")
software()
if answer in begin[0:2]:
print("hmm")
physical()

Python: matching a word in dictionary value

Is there a way to search a dictionary value to match a specific word in python 2.7. I tried making use of the any() command, but this doesn't return anything useful, am I on the right track?
dict = {'1' : 'see john run'}
test = 'run'
if any (x in test for x in Dict.values()):
print "true"
You got the right idea, but you need to switch the arguments in the in test
data = {'1' : 'see john run'}
test = 'run'
if any(test in x for x in data.values()):
print "true"
Just try the following:
print test in mydict.values()[0].split()
>>> mydict = {'1' : 'see john run'}
>>> test = 'run'
>>> print test in mydict.values()[0].split()
True
>>>
Also, i'd suggest changing the variable name dict to something else (I used mydict) because dict shadows the built-in.

English alphabet recognition in Python

I am planning to do a semester project on English alphabet recognition using Python. I am using Python for the first time. What are the tools and methodologies needed? Please help.
Your question is not clear and I suggest you rephrase it or risk having it put on hold.
However, if you mean checking whether a character in a string is a letter, you can use the built-in isaplha() method.
Examples:
>>> 'a'.isalpha()
True
>>> 'x'.isalpha()
True
>>> my_char = 'd'
>>> my_char.isalpha()
True
>>> '1'.isalpha()
False
>>> my_var = '#'
>>> my_var.isalpha()
False

Select Everything After Greater Than in Python

I have a string
"Tyson Invitational 02/08/2013','#FFFFCC')""; ONMOUSEOUT=""kill()"" >6.54"
How would I use regex to select everything after the right-pointing bracket? Aka how would I get the 6.54?
I've tried
\>(.*)
but I'm not sure it's working properly. I use
m = re.search( '\>(.*)', row_out[5])
and get
<_sre.SRE_Match object at 0x10b6105d0>
Not sure what the issue is.
Thanks!
>>> import re
>>> str="""Tyson Invitational 02/08/2013','#FFFFCC')""; ONMOUSEOUT=""kill()"" >6.54"""
>>> re.search('\>(.*)',str)
<_sre.SRE_Match object at 0x7f0de9575558>
same as you got before. However assign the search result to a variable and
>>> f=re.search('\>(.*)',str)
>>> f.groups()
('6.54',)