This question already has answers here:
Splitting string and removing whitespace Python
(2 answers)
Closed 9 years ago.
tgtPorts = str(options.tgtPort).split(', ')
I'm trying to split a string tgtPort that could look like 21, 80, 139
According to the website I was looking at, the above should split that string into a list containing each individual element IE: 139
However using:
for tgtPort in tgtPorts:
print tgtPort + "\n"
I find that my list only contains 21,
How can I ensure that the comma and the space are removed?
How can I ensure that all elements will end up in my list and not just the first one?
Spelling out Robert's advice:
tgtPorts = [s.strip() for s in str(options.tgtPort).split(',')]
Related
This question already has answers here:
Split string with multiple delimiters in Python [duplicate]
(5 answers)
Closed 2 years ago.
I think what I'm trying to achieve is fairly common but I can't find reference for it on the internet; either that or I'm misphrasing what I'm trying to do.
This is the string I would like to split:
array_1:target:radec, 0:00:00.00, -90:00:00.0
I would like to split it by the first two colons (':'), and by the first comma & space (', '), such that I get
['array_1', 'target', 'radec', '0:00:00.00, -90:00:00.0']
I've tried to run split() with arguments twice on the original string, and it fails on the second split() because I'm trying to split something that's already a list. All the other answers I can find seem to focus on splitting the string by all instances of a delimiter, but I want the last field in the list 0:00:00.00, -90:00:00.0 to remain like it is.
First split it by the first ", " (using maxsplit=1), then the first element of the resulting list split by ":":
s = "array_1:target:radec, 0:00:00.00, -90:00:00.0"
temp = s.split(", ", maxsplit=1)
temp[0] = temp[0].split(":")
result = temp[0] + [temp[1]]
The result:
['array_1', 'target', 'radec', '0:00:00.00, -90:00:00.0']
How about
l1 = s.split()
l2 = l1[0].split(':') + l1[1:]
This will first split by whitespace separator, then split the first element (only) by a colon separator, and then join the lists. Result:
['array_1', 'target', 'radec,', '0:00:00.00,', '-90:00:00.0']
This question already has answers here:
How to extract numbers from a string in Python?
(19 answers)
Closed 3 years ago.
So, I have a string "AB256+74POL". I want to extract the numbers only into a list say num = [256,74]. How to do this in python?
I have tried string.split('+') and followed by iterating over the two parts and adding the characters which satisfy isdigit(). But is there an easier way to that?
import re
a = 'AB256+74POL'
array = re.findall(r'[0-9]+', a)
"".join([c if c.isdigit() else " " for c in mystring]).split()
Explanation
Strings are iterable in python. So we iterate on each character in the string, and replace non digits with spaces, then split the result to get all sequences of digits in a list.
This question already has answers here:
Remove characters from beginning and end or only end of line
(5 answers)
Closed 4 years ago.
So, I have the following string "........my.python.string" and I want to remove all the "." until it gets to the first alphanumeric character, is there a way to achieve this other than converting the string to a list and work it from there?
You can use re.sub:
import re
s = "........my.python.string"
new_s = re.sub('^\.+', '', s)
print(new_s)
Output:
my.python.string
This question already has answers here:
How to get the position of a character in Python?
(11 answers)
Closed 7 years ago.
For example:
my_string = "hi how are you?/nIs everything ok?/nAre you happy?"
I need to make a list containing all the indexes of the newline - (/n).
How can i do it ?
You can use enumerate in a list comprehension to create a list of indices.
>>> [index for index, value in enumerate(my_string) if value == '\n']
[15, 33]
By the way, a new line character is '\n' not '/n' (note the slash)
import re
my_string = "hi how are you?/nIs everything ok?/nAre you happy?"
list = [m.start() for m in re.finditer('/n', my_string)]
This question already has answers here:
How to iterate over a list in chunks
(39 answers)
Closed 8 years ago.
I have a string "111222333" inside a CSV file. I would like to convert this into something like "\111\222\333"
Currently my python code is :
refcode = "111222333"
returnstring = "\\" + refcode[:3] + "\\" + refcode[3:6] + "\\" + refcode[-3:] + "\\"
I know there must be a better way to do this. May I know what are the better ways to do the same thing. Please help.
You could use re for that:
import re
refcode = "111222333"
returnstring = '\\'.join(re.match('()(\S{3})(\S{3})(\S{3})()', refcode).groups())
Explanation:
You have a string of 9 characters (let's say they are not any kind of whitespace chatacters, so we could represent it with \S).
We create a matching regexp using it, so (\S{3}) is a group of three sequential non-space characters (like letters, numbers, exclamation marks etc.).
(\S{3})(\S{3})(\S{3}) are three groups with 3 characters in each one.
If we call .groups() on it, we'll have a tuple of the matched groups, just like that:
In [1]: re.match('(\S{3})(\S{3})(\S{3})', refcode).groups()
Out[1]: ('111', '222', '333')
If we join it using a \ string, we'll get a:
In [29]: print "\\".join(re.match('(\S{3})(\S{3})(\S{3})', refcode).groups())
111\222\333
But you want to add the backslashes on the both sides of the string as well!
So we could create an empty group - () - on the each side of the regular expression.