Python Forum
strip space from end of a row of text
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
strip space from end of a row of text
#1
I am using Linux Mint 19.1 and Python 3.6.

Proofreading pages of text on the web, where each row ends with a space, followed by a newline. I need to replace (strip) the space at the end of the row.

My first question is: Is it necessary to iterate through the rows one by one, or can all rows be stripped at once by using .replace() ?

I tested the following code but it doesn't work.

import time

keyboard.send_keys("<ctrl>+a")
time.sleep(.5)
text = clipboard.get_selection() 
old = " " + "\n"
new = "\n"
text.replace(old, new)
keyboard.send_keys(text)
Also tried .strip() and that doesn't work either.
Linux Mint Cinnamon 21.3 - Python 3.10.12 - Autokey-gtk 0.96.0 as of 2024-01-13.
Reply
#2
String methods don't work in place, they return a modified version of the string. You need to assign that result back to text.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Apr-14-2019, 07:01 PM)ichabod801 Wrote: String methods don't work in place, they return a modified version of the string. You need to assign that result back to text.

Thanks ichabod801. How can I see the modifed returns?
Linux Mint Cinnamon 21.3 - Python 3.10.12 - Autokey-gtk 0.96.0 as of 2024-01-13.
Reply
#4
text = text.replace(old, new)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Apr-15-2019, 12:44 AM)ichabod801 Wrote: text = text.replace(old, new)

Much thanks ichabod801. It works fine.
Linux Mint Cinnamon 21.3 - Python 3.10.12 - Autokey-gtk 0.96.0 as of 2024-01-13.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extract only text strip byte array Pir8Radio 7 2,786 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
Smile please help me remove error for string.strip() jamie_01 3 1,145 Oct-14-2022, 07:48 AM
Last Post: Pedroski55
  Can't strip extra characters from Data Canflyguy 7 1,813 Jan-10-2022, 02:16 PM
Last Post: Canflyguy
  strip() pprod 8 3,369 Feb-16-2021, 01:11 PM
Last Post: buran
  saving data from text file to CSV file in python having delimiter as space K11 1 2,353 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  from global space to local space Skaperen 4 2,269 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  Need help with code for my WS2812B (Neopixel) Led Strip Phibbl 1 2,700 Apr-08-2020, 07:18 PM
Last Post: deanhystad
  split strip issues 'NonType' casacafe 8 3,758 Oct-08-2019, 06:29 PM
Last Post: ichabod801
  removing spaces/tabs after used .strip() zarize 0 1,552 Sep-11-2019, 12:46 PM
Last Post: zarize
  strip off just newlines Skaperen 11 5,219 Jun-19-2019, 06:28 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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