0

I would like to have the text printed out as how it shown on the exercise whereby the Lists of List has a * on each line and each are in a new line. I am still new to python and the Automate the Boring Stuff with Python book is kind of confusing sometimes.

I started by typing the text into the Python editor and having Pyperclip to copy it unto the clipboard. The problem is Pyperclip only accepts a single string, in which form the text is copied to the clipboard.

#! python3

#bulletPointerAdder.py - Adds Wikipedia bullet points to the start
#of each line of text on the clipboard.
#! python3

#bulletPointerAdder.py - Adds Wikipedia bullet points to the start #of each line of text on the clipboard.

In the Python shell:

import pyperclip
>>> text = 'Lists of monkeys Lists of donkeys Lists of pankeys'
>>> pyperclip.copy(text)
>>>
 RESTART: C:\Users\User\AppData\Local\Programs\Python\Python37-

32\bulletpointadder.py >>> text '* Lists of monkeys Lists of donkeys Lists of pankeys' >>>

import os
import pyperclip
text = pyperclip.paste()


#Separate lines and add starts.
lines = text.split(os.linesep)
for i in range(len(lines)): # loop through all indexes in the "lines"
list
    lines[i] = '* ' + lines[i] # add star to each sting in "lines" list

text = os.linesep.join(lines)
pyperclip.copy(text)

I actually want the text to be printed out like the sample below, but the problem is I am getting it print out as a single string.

  • Lists of animals
  • Lists of aquarium life
  • Lists of biologists by author abbreviation
  • Lists of cultivars
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

1

Understand this first and move to step 3:

We split the text along its newlines to get a list in which each item is one line of the text. We store the list in lines and then loop through the items in lines.

For each line, we add a star and a space to the start of the line. Now each string in lines begins with a star.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0
import pyperclip

text = pyperclip.paste()

# TODO manipulate the text in clipboard
lines = text.split('\n')                        # Each word is split into new line
for i in range(len(lines)):
    lines[i] = '* ' + lines[i]                  # Each word gets a * prefix
text = '\n'.join(lines)                         # all the newlines created are joind back
pyperclip.copy(text)                            # whole content is than copied into clipboard
print(text)

With this code if you copy a list of things it will still be a list of things as it is intended.

ElvisO
  • 1
  • 1