1

I have these two lines in the format

VP VB go
NP PRP$ your NN left

saved in a text file. I want to access this text file and then print this following result in a new text file

NP NN left

Help me how to this using python.

Thanks for any help in advance

2
  • 1
    On what basis are we suppose to select NP NN left from the text file? Without such an explanation, print('NP NN left') is a valid solution. Commented Mar 16, 2013 at 22:19
  • @unutbu I want to print this for all those pattern which have NP in the beginning of the line and NN before a word in that same line. Commented Mar 16, 2013 at 22:26

2 Answers 2

1

If I am interpreting you correctly, you want all cases of

NP NN word

in which case you could use a regex expression that looks for NP, NN, and the subsequent word:

import re
f = open('file.txt')
regex = r'^(NP).*?(NN) (\w+).*?$'
for line in f:
    try: ' '.join(re.search(regex, line).groups())
    except AttributeError: pass
Sign up to request clarification or add additional context in comments.

Comments

0

Edit: is this better?

f=open("myfile")
#read all lines of the file and remove newline characters
a=[i.strip() for i in f.readlines()]
f.close()

for i in a:
  i=i.split()
  n=-1
  try:
    n=i.index("NN")
  except:
    pass
  if n!=-1 and n!=len(i)-1 and i[0]=="NP":
    print i[0], i[n], i[n+1]

1 Comment

the file have 1000 lines of those types and the number of words between NP and NN is not fixed so printing using array by giving index and specifying that a in first line won't be that possible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.