Removing a digit from a string

Question:
I am using the turtle module to right a poem. The website i use to get these poems from has a digit in the string and i would like to remove the digit.

Repl link:
https://replit.com/@jonathanessombe/new-attempt#main.py

import turtle
import re

Tu = turtle.Turtle()
Tu.shape('turtle')
screen = turtle.Screen()
screen.setup(1000, 1000)
Tu.penup()

def write_poem(poem_lines):

  y_initial = 400
  x_initial = -300

  last_line = poem_lines.pop(0)
  poem_lines.append(last_line)
  
  last_line_regex = '.+(by)\s+[a-zA-Z.]+(\s+[a-zA-Z.]+)*'
  pattern = re.fullmatch(last_line_regex, poem_lines[-1])
 
  if pattern:
    Tu.goto(x_initial, y_initial)
    if poem_lines[-1].isdigit():
      unwanted_number = str(poem_lines[-1].lstrip(' 123456789.'))
      poem_lines.append(unwanted_number)
    for lines in poem_lines:
      Tu.write(lines)
      y_initial -= 20
      Tu.goto(x_initial , y_initial) 
      
        
  else:  
    Tu.goto(-50, 0)
    Tu.write('      please enter a poem with a title name and an author')

with open('poem_canvas.txt', 'r') as poem_canva:
  poem_line = poem_canva.read()
  poem_sentence = [sentence for sentence in poem_line.split('\n') if sentence != '' ]

write_poem(poem_sentence)

input()```
2 Likes

Can you give a couple example poems?

2 Likes

The isdigit() method checks if the entire string consists of digits, which I believe is not what you want in this case.

You can use the re.sub method which will remove the digits and a additional part to remove from the last line of the poem

  last_line_regex = '.+(by)\s+[a-zA-Z.]+(\s+[a-zA-Z.]+)*'
  pattern = re.fullmatch(last_line_regex, poem_lines[-1])

  if pattern:
    Tu.goto(x_initial, y_initial)
    # So here you substitute any digits in the last line with an empty string
    poem_lines[-1] = re.sub(r'\d+', '', poem_lines[-1])
    for lines in poem_lines:
      Tu.write(lines, font=("Arial", 16, "normal"))  # You can left just the lines or add a font just to make it better visuallly
      y_initial -= 20
      Tu.goto(x_initial, y_initial)
2 Likes

poem example from https://www.bookbub.com/blog/best-short-poems

#poem_canvas.text file

2. “This Room” by John Ashbery
The room I entered was a dream of this room.
Surely all those feet on the sofa were mine.
The oval portrait
of a dog was me at an early age.
Something shimmers, something is hushed up.

We had macaroni for lunch every day
except Sunday, when a small quail was induced
to be served to us. Why do I tell you these things?
You are not even here.

3. “Hesitations Outside the Door” by Margaret Atwood
I’m telling the wrong lies,
they are not even useful.

The right lies would at least
be keys, they would open the door.

The door is closed; the chairs,
the tables, the steel bowl, myself

shaping bread in the kitchen, wait
outside it.






Did you tried what I told you?

No not yet, i will let you know when i do it

1 Like
import turtle
import re

Tu = turtle.Turtle()
Tu.shape('turtle')
screen = turtle.Screen()
screen.setup(1000, 1000)
Tu.penup()

def write_poem(poem_lines):

  y_initial = 400
  x_initial = -300

  last_line = poem_lines.pop(0)
  poem_lines.append(last_line)

  last_line_regex = '.+(by)\s+[a-zA-Z.]+(\s+[a-zA-Z.]+)*'
  pattern = re.fullmatch(last_line_regex, poem_lines[-1])

  if pattern:
    Tu.goto(x_initial, y_initial)
    poem_lines[-1] = re.sub('\d+.', '', poem_lines[-1],count=1)
    for lines in poem_lines:
      Tu.write(lines, font=('Ariel', 16, 'normal'))
      y_initial -= 20
      Tu.goto(x_initial , y_initial) 


  else:  
    Tu.goto(-50, 0)
    Tu.write('      please enter a poem with a title name and an author')

with open('poem_canvas.txt', 'r') as poem_canva:
  poem_line = poem_canva.read()
  poem_sentence = [sentence for sentence in poem_line.split('\n') if sentence != '' ]

write_poem(poem_sentence)

input()

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.