This question already has answers here:
How can I split and parse a string in Python? [duplicate]
(3 answers)
Closed 5 years ago.
I need to split the string everytime ; shows up.
words = "LightOn;LightOff;LightStatus;LightClientHello;"
Output should be something like this:
LightOn
LightOff
LightStatus
LightClientHello
Simply, everytime it finds ; in a string, it has to split it.
Thank you for help
res = words.split(";")
Refer to this link for more information on split.
Related
This question already has answers here:
Decode HTML entities in Python string?
(6 answers)
Closed 3 years ago.
I have a string "hello[ World]" and I want to convert it to "hello[World]"
I tried something like this:
a.encode("utf-8").decode("ascii")
I got back same string as input.
Try this:
import html
html.unescape("LASIX [FUROSEMIDE]")
This produces:
'LASIX [FUROSEMIDE]'
This question already has answers here:
How do I remove a substring from the end of a string?
(23 answers)
Closed 5 years ago.
I want to strip the substring '_pf' from a list of strings. It is working for most of them, but not where there is a p in the part of the string I want to remain. e.g.
In: x = 'tcp_pf'
In: x.strip('_pf')
Out:
'tc'
I would expect the sequence above to give an output of 'tcp'
Why doesn't it? Have i misunderstood the strip function?
you can use:
x = 'tcp_ip'
x.split('_ip')[0]
Output:
'tcp'
You can also use spilt function like below,
x.split('_pf')[0]
It will give you tcp.
This question already has answers here:
What's the u prefix in a Python string?
(5 answers)
Closed 6 years ago.
I am trying to parse the 'Meghan' part from the line:
link = http://python-data.dr-chuck.net/known_by_Meghan.html
...with the following regex:
print re.findall('by_(\S+).html$',link)
I am getting the output:
[u'Meghan']
Why I am getting the 'u'?
It means unicode. Depending on what you'll do with it, you can ignore it for the most part, of you can convert it to ascii by doing .encode('ascii')
This question already has answers here:
How to get part of string and pass it to other function in python?
(4 answers)
Closed 8 years ago.
A beginner question
My file looks like -->
10.5.5.81=apache,php,solr
10.5.5.100=oracle,coherence
How can I cut the IP part and store it into a list for further processing?
Please help.
answer = []
with open('path/to/file') as infile:
for line in infile:
answer.append(line.partition('=')[0].strip())
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Python strings split with multiple separators
Is there a way in Python to split a string on two different keys?
Say I have a string like this:
mystr = "wketjwlektjwltjkw<br/>wwwweltjwetlkwjww" + \
"wwetlwjtwlet<strong>wwwwketjwlektjwlk</strong"
I'd like to split on EITHER <br/>wwww or <strong>wwww.
How should I do this?
Thanks!
import re
re.split(r'<(br\/|strong)>wwww', mystr)[::2]