0
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?

Juicy
  • 11,840
  • 35
  • 123
  • 212
  • 2
    Split on just the comma (not comma+space), and trim the individual strings. – Robert Harvey Jan 21 '14 at 23:45
  • 2
    Your code does not demonstrate your problem. You can see it running [here](http://repl.it/OCU). The output is `21` then a blank line then `80` then a blank line then `139` then a blank line. No commas anywhere. – abarnert Jan 21 '14 at 23:50

1 Answers1

3

Spelling out Robert's advice:

tgtPorts = [s.strip() for s in str(options.tgtPort).split(',')]
Mike DeSimone
  • 41,631
  • 10
  • 72
  • 96