python parsing string using regex [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to parse a string from this:
CN=ERT234,OU=Computers,OU=ES1-HER,OU=ES1-Seura,OU=RES-ES1,DC=resu,DC=kt,DC=elt
To this:
ES1-HER / ES1-Seura
Any easy way to do this with regex?

>>> import re
>>> s = 'CN=ERT234,OU=Computers,OU=ES1-HER,OU=ES1-Seura,OU=RES-ES1,DC=resu,DC=kt,DC=elt'
>>> re.findall('OU=([^,]+)', s)
['Computers', 'ES1-HER', 'ES1-Seura', 'RES-ES1']
>>> re.findall('OU=([^,]+)', s)[1:3]
['ES1-HER', 'ES1-Seura']
>>> ' / '.join(re.findall('OU=([^,]+)', s)[1:3])
'ES1-HER / ES1-Seura'
Don't use str as a variable name. It shadows builtin function str.

Related

Reverse and split string: Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to reverse and split a string in Python. Please suggest how?
'this is Xing Min' should return ['niM', 'gniX', 'si', 'siht'].
You can do it as:
my_str[::-1].split()
Example
>>> s = 'Hello World'
>>> print s[::-1].split()
['dlroW'. 'olleH']
>>> s = 'this is Xing Min'
>>> print s[::-1].split()
['niM', 'gniX', 'si', 'siht']
Here, the [::-1] gets the whole string in reverse order. This is the syntax [start:end:step]. When you don't specify a start and end, it will deal with the whole string. When you do [::-1], the step value is -1 which means that the string is read in reverse.

Check if part of text is in tuple [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to check if part of text is in tuple?
For example:
my_data = ((1234L,), (23456L,), (3333L,))
And we need to find if 123 or 1234 is in tuple.
I didn't worked lot with tuples before.
In array we use:
if variable in array
But its not working for tuples like my_data
PS. 1st answer solved problem.
def findIt(data, num):
num = str(num)
return any(num in str(i) for item in data for i in item)
data = ((1234L,), (23456L,), (3333L,))
print findIt(data, 123)
Output
True

How to find every item of the list in a string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
strSpecialChars=['%', 'dBu', 'dB', 'kHz', 'Hz']
str = "-20.0dB"
I need to get True here as it checks for each item of the list - strSpecialChars in the string str.
Use the any() function to test each value:
>>> strSpecialChars=['%', 'dBu', 'dB', 'kHz', 'Hz']
>>> yourstr = "-20.0dB"
>>> any(s in yourstr for s in strSpecialChars)
True
where I renamed str to yourstr to avoid masking the built-in type.
any() will only advance the generator expression passed to it until a True value is returned; this means only the first 3 options are tested for your example.
You could use str.endswith() here:
any(yourstr.endswith(s) for s in strSpecialChars)
to limit matches to only those that end with any of the special characters.
map(lambda s: s in "-20.0dB", strSpecialChars)
You may need to convert the output through list to actually see it.

how to replace specific characters with different rules with python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a file and some substitution is needed: replace "," with "," and all other characters not in rule2 with a whitespace, how can I do that?
What about this?
text = text.replace(",", ",")
You can use the regular expressions module:
text = re.sub(',', ',', text)
text = re.sub(negated_rule2, ' ', text)
where your negation of "rule2" is formatted using the regular expressions syntax (see link above).

how to convert timecode into seconds:fractions_of_a_second? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Given timecode in this format
00:00:00,0
What is the best way to convert this into seconds.fractions_of_a_second?
Here's a link for some python code to handle SMPTE timecodes http://code.google.com/p/pytimecode/
Hope it helps...
The datetime module is your friend in this case
import datetime
time_string = "17:48:12,98"
t = datetime.datetime.strptime(time_string, "%H:%M:%S,%f")
seconds = 60 * t.minute * t.hour
print (seconds, t.microsecond)

Categories

Resources