Search code examples
python-3.xpyperclip

Using Pyperclip in Python 3 Does Not Paste Data in Desired Format


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.


Solution

  • Try this way:

    import pyperclip
    
    text = pyperclip.paste()
    lines = text.split()
    pyperclip.copy(', '.join(lines))