Question

My inputfile(i.txt) is given below:

പ്രധാനമന്ത്രി മന്‍മോഹന്‍സിംഗ്  നാട്ടില്‍ എത്തി  .  
അദ്ദേഹം മലയാളി അല്ല  കാരണം അദ്ദേഹത്തെ പറ്റി പറയാന്‍  വാക്കുകല്ളില്ല . 

and my connectives are in the list:

connectives=['കാരണം','അതുകൊണ്ട്‌ ','പക്ഷേ','അതിനാല്‍','എങ്കിലും','എന്നാലും','എങ്കില്‍','എങ്കില്‍പോലും',
'എന്നതുകൊണ്ട്‌ ','എന്ന']  

My desired output is(outputfile.txt):

പ്രധാനമന്ത്രി മന്‍മോഹന്‍സിംഗ്  നാട്ടില്‍ എത്തി  .  
അദ്ദേഹം മലയാളി അല്ല . 
അദ്ദേഹത്തെ പറ്റി പറയാന്‍  വാക്കുകല്ളില്ല . 

If there are 2 connectives split according to that. My code is:

fr = codecs.open('i.txt', encoding='utf-8') 
fw = codecs.open('outputfile.txt', 'w')
for line in fr:
    line_data=line.split()
for x, e in list(enumerate(line_data)):
    if e in connectives:
        line_data[x]='.' 

The code is not completed.

Was it helpful?

Solution

I think you just have some indentation problems. I also added u'' to the connectives to specify unicode since I am using python 2.7.

You need to maybe add a carriage return with the . if you want it to split an existing line into two lines...

Here is a start (but not final):

import codecs

connectives=[u'കാരണം',u'അതുകൊണ്ട്‌ ',u'പക്ഷേ',u'അതിനാല്‍',u'എങ്കിലും',u'എന്നാലും',u'എങ്കില്‍',u'എങ്കില്‍പോലും',
u'എന്നതുകൊണ്ട്‌ ',u'എന്ന']  

fr = codecs.open('i.txt', encoding='utf-8') 
# fw = codecs.open('outputfile.txt', 'w')
for line in fr:
    line_data=line.split()
    for x, e in list(enumerate(line_data)):
        if e in connectives:
            line_data[x]='.\n'

    print " ".join(line_data).lstrip()

Generates this output (extra space because the split comes in the middle of a line).

പ്രധാനമന്ത്രി മന്‍മോഹന്‍സിംഗ് നാട്ടില്‍ എത്തി .
അദ്ദേഹം മലയാളി അല്ല .
 അദ്ദേഹത്തെ പറ്റി പറയാന്‍ വാക്കുകല്ളില്ല .

OTHER TIPS

Here's one way you could do it, building up a string word by word and adding .\n where appropriate:

#!/usr/bin/python
# -*- coding: utf-8 -*-

connectives=set(['കാരണം','അതുകൊണ്ട്‌ ','പക്ഷേ','അതിനാല്‍','എങ്കിലും','എന്നാലും',
                 'എങ്കില്‍','എങ്കില്‍പോലും','എന്നതുകൊണ്ട്‌ ','എന്ന', '.'])

s=""

with open('i.txt') as file:
    for line in file:
        for word in line.split():
            if word in connectives:                
                s += '.\n'
            else:
                s += '{} '.format(word)

print s

Note that I added the '.' to the end of the connectives list and made it into a set. Sets are a type of collection that are useful for fast membership testing, such as if word in connectives: in the code. I also decided to use str.format to put the word into the string. This could be changed for word + ' ' if preferred.

Output:

പ്രധാനമന്ത്രി മന്‍മോഹന്‍സിംഗ് നാട്ടില്‍ എത്തി .
അദ്ദേഹം മലയാളി അല്ല .
അദ്ദേഹത്തെ പറ്റി പറയാന്‍ വാക്കുകല്ളില്ല .

Unlike the other answer, there's no problem with the leading whitespace at the start of each line after the first one.

By the way, if you are comfortable using list comprehensions, you could condense the code down to this:

#!/usr/bin/python
# -*- coding: utf-8 -*-

connectives=set(['കാരണം','അതുകൊണ്ട്‌ ','പക്ഷേ','അതിനാല്‍','എങ്കിലും','എന്നാലും',
                 'എങ്കില്‍','എങ്കില്‍പോലും','എന്നതുകൊണ്ട്‌ ','എന്ന', '.'])

with open('i.txt') as file:
    s = ''.join(['.\n' if word in connectives else '{} '.format(word) 
                 for line in file 
                 for word in line.split()])

print s
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top