-6

I have a file containing words, I want to read this file and add a label in front of all words. The label should be added on the right side of the words. eg. book - "O", Berlin - "O". How to do it in python? I have tried this code but not given my answer.

inp = open('Dari.pos', 'r')
out = open('DariNER.txt', 'w')

for line in iter(inp):
    word= line.__add__("O")
    out.write(word)
inp.close()
out.close()
The Afghan
  • 99
  • 1
  • 7
  • 2
    First you read the text from the file, then add the label in front and finally save it. Can you show us what you have tried so far? – ViG Feb 06 '18 at 13:50
  • As @ViG said, show us what you have tried so far. Additionally, your question is highly vague! Can you please show as the inputs and what you expect as the outputs? – Abhishek Kumar Feb 06 '18 at 13:51
  • Actually, I can read a file but I dont know how to add a label in front of each word. – The Afghan Feb 06 '18 at 13:52
  • define label? hello -> labelhello? label, hello? label-hello? [(label, hello)]? – Tzomas Feb 06 '18 at 13:54
  • Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it **in the question itself**. Questions without a **clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Tom Dalton Feb 06 '18 at 13:55
  • I have a list of words, like books, shops .... Just I want to add in front of books a tab space then "O" in all words. – The Afghan Feb 06 '18 at 13:56
  • @TomDalton, the question is clear, and I have replied to other comments. Just add a label to each word in a file.but don't know how to do it – The Afghan Feb 06 '18 at 13:59
  • 2
    You have not provided any code, nor the specific problem you have with that code, nor the specific output you want. You are expecting us to donate our time to help you, it is in your interest to make it easy for us to help you. At the moment, it's unclear to me what you want or what you have tried, and so I can't help you. – Tom Dalton Feb 06 '18 at 14:01
  • @TomDalton I added the code, and it works now. Just let me know how to add it on the right side of the words with a tab space. – The Afghan Feb 06 '18 at 14:05
  • 1
    Thanks for editing the question to include your code. Can you now edit the question again to include examples of lines from the input file and how you want them to appear in the output file? – quamrana Feb 06 '18 at 14:16
  • @quamrana, Thanks, I did, can you have a look on the code now. – The Afghan Feb 06 '18 at 14:25
  • @TomDalton, please have a look now. – The Afghan Feb 06 '18 at 14:26
  • @Tzomas, Can you have a look now – The Afghan Feb 06 '18 at 14:26

2 Answers2

0

If I understand the correct output format word-O, you can try something like this:

words = open('filename').read().split()
labeled_words = [word+"-O" for word in words]

# And now user your output format, each word a line, separate by tabs, whatever.
# For example new lines
with open('outputfile','w') as output:
    output.write("\n".join(labeled_words))
Tzomas
  • 704
  • 5
  • 17
0

In your updated question you show examples of words (I assume you mean lines) with some characters added to them:

eg. book - "O", Berlin - "O"

This modification to your code should produce that output:

for line in iter(inp):
    word = '{} - "O"'.format(line)
    out.write(word)

I ran a test with this code below:

inp = ['This is a book','I bought it in Berlin']

for line in iter(inp):
    word = '{} - "O"'.format(line)
    print(word)

Output:

This is a book - "O"
I bought it in Berlin - "O"
quamrana
  • 37,849
  • 12
  • 53
  • 71