Python Forum
My first project as a beginner
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My first project as a beginner
#1
Hi.

I'm trying to learn Python by reading the book called "Automate the boring stuff", and have written my first project which I have copied from the book.

It's supposed to copy text from a clipboard, and add (*) in front of each line, but when I run it nothing happens. I have copied a random text on a website with Ctrl+C. Not sure if this is the right way to do it Shy
Can someone tell me what I'm doing wrong?

My code:
#! python 3
# bulletPointAdder.py - Adds Wikipedia bullet points to the start
# of each line of text on the clipboard.

import pyperclip
text = pyperclip.paste()

# Separate lines and add stars.
lines = text.split('\n')
for i in range(len(lines)):     # loop through all indexes in the "lines" list
    lines[i] = '* ' + lines[i]  # add star to each string in "lines" list
text = '\n'.join(lines)
pyperclip.copy(text)
Error:
Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> = RESTART: C:\Users\Lenovo\Documents\Python\Automate_the_boring_stuff\Projects\bulletPointAdder.py >>>
Reply
#2
The code dos not output anything only manipulate text that is in Clipboard in Windows
If add print(text) will see the output.
# py_copyclip.py
import pyperclip
text = pyperclip.paste()

# Separate lines and add stars.
lines = text.split('\n')
for i in range(len(lines)):
    lines[i] = '* ' + lines[i]
text = '\n'.join(lines)
print(text)
pyperclip.copy(text)
Copy this text.
Output:
hello world 12345
Run code,will now see the output.
G:\div_code
λ python py_copyclip.py
* hello world
* 12345
Paste from clipboard (Ctrl+v)
Output:
* hello world * 12345
lil_e likes this post
Reply
#3
Why the error post? I don't see any error and your code works fine for me.

After running your program, paste the clipboard contents into something. This is what shows up after I copied your program to the clipbaord, pasted to VSCode, ran it, and then did a paste here:
Output:
* import pyperclip * text = pyperclip.paste() * * # Separate lines and add stars. * lines = text.split('\n') * for i in range(len(lines)): # loop through all indexes in the "lines" list * lines[i] = '* ' + lines[i] # add star to each string in "lines" list * text = '\n'.join(lines) * pyperclip.copy(text)
My God, it's full of stars!

The code could also be written using str.replace()
import pyperclip

text = "*" + pyperclip.paste().replace("\n", "\n*")
pyperclip.copy(text.rstrip("*"))
Or using an f'string and a comprehension.
import pyperclip

lines = (f'*{line}' for line in pyperclip.paste().split("\n")
pyperclip.copy("\n".join(lines))
And probably many, many more.
lil_e likes this post
Reply
#4
(Feb-26-2023, 06:38 PM)snippsat Wrote: The code dos not output anything only manipulate text that is in Clipboard in Windows
If add print(text) will see the output.
# py_copyclip.py
import pyperclip
text = pyperclip.paste()

# Separate lines and add stars.
lines = text.split('\n')
for i in range(len(lines)):
    lines[i] = '* ' + lines[i]
text = '\n'.join(lines)
print(text)
pyperclip.copy(text)
Copy this text.
Output:
hello world 12345
Run code,will now see the output.
G:\div_code
λ python py_copyclip.py
* hello world
* 12345
Paste from clipboard (Ctrl+v)
Output:
* hello world * 12345

Thanks!! Smile
Reply
#5
(Feb-26-2023, 11:35 PM)deanhystad Wrote: Why the error post? I don't see any error and your code works fine for me.

After running your program, paste the clipboard contents into something. This is what shows up after I copied your program to the clipbaord, pasted to VSCode, ran it, and then did a paste here:
Output:
* import pyperclip * text = pyperclip.paste() * * # Separate lines and add stars. * lines = text.split('\n') * for i in range(len(lines)): # loop through all indexes in the "lines" list * lines[i] = '* ' + lines[i] # add star to each string in "lines" list * text = '\n'.join(lines) * pyperclip.copy(text)
My God, it's full of stars!

The code could also be written using str.replace()
import pyperclip

text = "*" + pyperclip.paste().replace("\n", "\n*")
pyperclip.copy(text.rstrip("*"))
Or using an f'string and a comprehension.
import pyperclip

lines = (f'*{line}' for line in pyperclip.paste().split("\n")
pyperclip.copy("\n".join(lines))
And probably many, many more.

Thank you!! Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with this project / Beginner kurwa97 2 1,832 Nov-13-2019, 11:11 PM
Last Post: kurwa97
  Open source project that a beginner could contribute to? JJJame 16 11,607 Apr-15-2017, 06:13 PM
Last Post: Low_Ki_

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020