Domanda

My input file is:

അദ്ദേഹം --> മന്‍മോഹന്‍സിംഗ്  
അദ്ദേഹത്തെ --> അദ്ദേഹം --> മന്‍മോഹന്‍സിംഗ് 
അവന്‍ --> രാമന്‍ 
അവള്‍ക്ക് --> സീതയെ 
അവള്‍ --> അവള്‍ക്ക് --> സീതയെ 
അത്‌ --> പൂവ്‌ 
അവര്‍ --> അവള്‍ക്ക് --> സീതയെ , അവന്‍ --> രാമന്‍ , ലക്ഷ്മണന്‍
അവിടെ --> കോട്ടയത്ത്‌ 
അവര്‍ക്ക്‌ --> മന്‍മോഹന്‍സിംഗ് , അവള്‍ക്ക് --> സീതയെ , ലക്ഷ്മണന്‍ , അവന്‍ --> രാമന്‍ 
അവിടെ --> അവിടെ --> കോട്ടയത്ത്‌ 
അവന്‍ --> ലക്ഷ്മനനെ 
അവനെ --> ലക്ഷ്മണന്‍ 
അവള്‍ക്ക്‌ --> സീതയെ 
അവന്‍ --> രാമന്‍ 

My desired outputfile contains :

അദ്ദേഹം --> മന്‍മോഹന്‍സിംഗ്  
അദ്ദേഹത്തെ --> മന്‍മോഹന്‍സിംഗ് 
അവന്‍ --> രാമന്‍ 
അവള്‍ക്ക് --> സീതയെ 
അവള്‍ --> സീതയെ 
അത്‌ --> പൂവ്‌ 
അവര്‍ --> സീതയെ,രാമന്‍ ,ലക്ഷ്മണന്‍
അവിടെ --> കോട്ടയത്ത്‌ 
അവര്‍ക്ക്‌ --> മന്‍മോഹന്‍സിംഗ് ,സീതയെ,ലക്ഷ്മണന്‍ ,രാമന്‍ 
അവിടെ --> കോട്ടയത്ത്‌ 
അവന്‍ --> ലക്ഷ്മനനെ 
അവനെ --> ലക്ഷ്മണന്‍ 
അവള്‍ക്ക്‌ --> സീതയെ 
അവന്‍ --> രാമന്‍ 

ie, If there is more than 1 '-->' character, the line need modifications. So my code starts from the position 2 in the list because first '-->' is needed. And check the current and next item in the list.But it didnot work. My code is:

fq = codecs.open('resolvedanaphora.txt', encoding='utf-8')
lines = fq.readlines()
fq.close()
for line in lines:
if ',' in line:
    words=[]    
    words=line.split()  
    for i in range(2,len(words)):
        if '-->' in words[i+1]:
            print 'yes'
            words[i]=''
            words[i+1]=''
    for w in words:
        fa.write(w)
    while len(words) > 0 : words.pop()

    else:
        fa.write(line)
    fa.write('\n')

fa.close()

I got a blank output file. And an error is displayed in terminal. The error is

if '-->' in words[i+1]:  
IndexError: list index out of range
È stato utile?

Soluzione 4

I got the correct output. I will look into the answers given above. My code is given below.

import codecs
fq = codecs.open('resolvedanaphora.txt', encoding='utf-8')
lines = fq.readlines()
fq.close()
fa = codecs.open('resolvedana.txt', 'w')
for i in lines:
    words=i.split()
    for i in range(2,len(words)):
            if words[i]=='-->':
                words[i]=''
                words[i-1]= ''
    for w in words:
        if w!='':
            fa.write(w.encode('UTF-8'))
            fa.write(' ')
    fa.write('\n')
fa.close()  

If a item in list is '-->' then the current item and previous item is replaced by ''. Since '' is not needed to be written in the output file, it is ignored.

The output is :

അദ്ദേഹം --> മന്‍മോഹന്‍സിംഗ്
അദ്ദേഹത്തെ --> മന്‍മോഹന്‍സിംഗ്
അവന്‍ --> രാമന്‍
അവള്‍ക്ക് --> സീതയെ
അവള്‍ --> സീതയെ
അത്‌ --> പൂവ്‌
അവര്‍ --> സീതയെ , രാമന്‍
അവിടെ --> കോട്ടയത്ത്‌
അവര്‍ക്ക്‌ --> സീതയെ , രാമന്‍
അവിടെ --> കോട്ടയത്ത്‌
അവന്‍ --> ലക്ഷ്മനനെ
അവനെ --> ലക്ഷ്മണന്‍
അവള്‍ക്ക്‌ --> സീതയെ
അവന്‍ --> രാമന്‍

Altri suggerimenti

import codecs
with codecs.open('out.txt', 'w') as fa:
    with open('in.txt') as fhandle:
        for line in fhandle:
            i = line.find('-->') + 3
            line = line[:i] + re.sub(r'\b\w+\s*-->', '', line[i:])
            fa.write(line)

Your character set didn't work for me. So, I tried as input:

a-->b
def-->hij-->klm
123-->456 ,789-->qwe,rty, uio-->pas

The output is:

a-->b
def-->klm
123-->456 ,qwe,rty, pas

The above code works by looking for the first occurrence of -->. All text up to and including the first occurrence of --> is left unchanged. In the text which follows, a substitution is made: any occurrence of a word followed by --> is removed. The resulting line is then written to file.

Although harmless here, file is a reserved word. So, the new code uses fhandle in place of file.

Notes on original code

  • line.split() splits the line on white space. That does not work for your input because of (a) the commas, and (b) the --> characters are not always surrounded by white space.

  • The loop for w in words: fa.write(w) strings all the words together with no spaces.

split splits on white space.

>>> line = 'അവര്‍-->അവള്‍ക്ക്-->സീതയെ,അവന്‍-->രാമന്‍ ,ലക്ഷ്മണന്‍'
>>> line.split()
['അവര്‍-->അവള്‍ക്ക്-->സീതയെ,അവന്‍-->രാമന്‍', ',ലക്ഷ്മണന്‍']

You probably want to split based on commas, then split based on '-->'. If you get more than two elements, just keep the first and last, and join with '-->'.

>>> line.split(',')
['അവര്‍-->അവള്‍ക്ക്-->സീതയെ', 'അവന്‍-->രാമന്‍', 'ലക്ഷ്മണന്‍']

I don't know what you want to do with the other elements but we'll just look at the first sub element.

>>> sub = 'അവര്‍-->അവള്‍ക്ക്-->സീതയെ'
>>> links = sub.split('-->')
>>> links
['അവര്‍', 'അവള്‍ക്ക്സീ', 'തയെ']

>>> if len(links) > 2:
...    collapsed = [links[0], links[-1]]

>>> collapsed
['അവര്‍', 'തയെ']

We can join the elements in the array with '-->' separating them:

>>> link = '-->'.join(collapsed)
>>> link
'അവര്‍-->തയെ'

If you want to join back with the other elements:

>>> ' ,'.join([link, 'അവന്‍-->രാമന്‍', 'ലക്ഷ്മണന്‍'])
'അവര്‍-->തയെ ,അവന്‍-->രാമന്‍ ,ലക്ഷ്മണന്‍'

instead of that method try this to replace all--> except first occurance

for s in lines:
        substr = "-->"
        try:
            first_occurrence = s.index(substr) + len(substr)
        except ValueError:
            pass
        else:
            s = s[:first_occurrence] + re.sub(r'\b\w+\s*-->', '', s[first_occurrence:])
        print s
input
a-->b
b-->c,b-->d,b-->e
c->d

output

a-->b
b-->c,d,e
c-->d
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top