0

I'm using python 3.7 and I want to:

  • Copy IPs from a column in excel
  • Add a comma between each IP separated by a space
  • Return as one line
  • Copy back to clipboard using pyperclip.

Below is the desired pasted results:

10.10.10.10, 10.10.10.11, 10.10.10.12, 10.10.10.13, 10.10.10.14

I have looked at some answers I found here and here but it's not printing the desired results. Below, I have tried the following codes but none seem to do it for me:

#code 1
import pyperclip
text = pyperclip.paste()
lines = text.split('\n')
pyperclip.copy('\n'.join(lines))

#code2
import pyperclip
text = pyperclip.paste()
lines = text.split('\n')
a = '{}'.format(', '.join(lines[:-1]))
pyperclip.copy(''.join(a))

#code3
import pyperclip
text = pyperclip.paste()
lines = text.split('\n')
a = '{}'.format(', '.join(lines[:-1]))
pyperclip.copy(a)

Assistance on fixing this and understanding would be greatly appreciated.

Nina G
  • 269
  • 5
  • 17

1 Answers1

0

Try this way:

import pyperclip

text = pyperclip.paste()
lines = text.split()
pyperclip.copy(', '.join(lines))
bruce
  • 462
  • 6
  • 9
  • Thanks! I was so focused on the last 2 lines that I overlooked the '\n' on the lines variable. – Nina G Sep 17 '19 at 18:59