Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy text with newlines to clipboard :Python

I want to copy text with newlines to the clipboard using the following code.

import pyperclip
numbers=''
for i in range(200):
    numbers = numbers + str(i) + '\n'
pyperclip.copy(numbers)

After execution the clipboard should contain:

0
1
2
.
.
200

But after I run the program and paste in Notepad. I see

012345....200

All in a single line. :( I use Python 3.6.1 on Windows 10

like image 225
Piyush Chauhan Avatar asked Oct 28 '25 16:10

Piyush Chauhan


1 Answers

I assume you're pasting into microsoft notepad. In this case you should use \r\n (windows style) instead of \n only (unix style)

If you paste into notepad++ it will treat the newline character unix style, and you will see it separated like you want.

If you want to be OS independent then use os.linesep

like image 81
EarthDragon Avatar answered Oct 31 '25 06:10

EarthDragon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!