0

How to ignore text inside (). In below example I have to ignore printing directions) & Over right).

Example:

Text = "A paragraph is a self-contained unit of discourse in writing dealing with a particular point or idea. A paragraph consists of one or more sentences. Though not required by the syntax of any language, paragraphs are usually an expected part of formal writing, used to organize longer prose.The oldest classical British and Latin writing had little or no space between words and could be written in boustrophedon (alternating. directions). Over time, text direction (left to. right) became standardized, and word dividers and terminal punctuation became common."

Code I used:

for x in text.split('. '):
    y=x.split(" ")
    print(y[0])

Output for this code:

A   A  Though directions) Over right)
Yuchen Ren
  • 287
  • 5
  • 13
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Apr 27 '22 at 08:00
  • What do you want exactly? Is this output you want or there is some thing else? Your question is not clear – Zain Ul Abidin Apr 27 '22 at 11:04

2 Answers2

0

You should use the re module that comes with Python. You want to substitute all instances of '(<any character(s)>)' with the empty string ''.

Try the following:

text1 = re.sub('\(.*\)', '', Text)

will generate a text that will not contain anything within parenthesis. The output of the above will be:

'A paragraph is a self-contained unit of discourse in writing dealing with a particular point or idea. A paragraph consists of one or more sentences. Though not required by the syntax of any language, paragraphs are usually an expected part of formal writing, used to organize longer prose.The oldest classical British and Latin writing had little or no space between words and could be written in boustrophedon  became standardized, and word dividers and terminal punctuation became common.'
ssm
  • 5,277
  • 1
  • 24
  • 42
0

Try this out:

import re
Text = "Your Text here"
Text = re.sub("\\(.*\\)","",Text)  # remove all items in the parenthesis
for x in Text.split('.'):   # split by the period
    x = x.strip()    # strip the spaces away 
    y=x.split(" ")
    print(y[0])
PurpleHacker
  • 358
  • 2
  • 9