简体   繁体   English

在文件的各行中找到一个单词,并将其分为两行

[英]Find a word in the lines in file and split it into two lines

My inputfile(i.txt) is given below: 我的输入文件(i.txt)如下:

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

and my connectives are in the list: 我的连接词在列表中:

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

My desired output is(outputfile.txt): 我想要的输出是(outputfile.txt):

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

If there are 2 connectives split according to that. 如果有2个连接词,则将其拆分。 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. 代码未完成。

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. 由于我使用的是python 2.7,因此我还向u''添加了u''以指定unicode。

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). 生成此输出(多余的空间,因为拆分位于一行的中间)。

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

Here's one way you could do it, building up a string word by word and adding .\\n where appropriate: 这是您可以执行的一种方法,一个字一个字地建立一个字符串,并在适当的地方添加.\\n

#!/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 . connectives列表的末尾,并使其成为set Sets are a type of collection that are useful for fast membership testing, such as if word in connectives: in the code. 集合是一种集合类型,可用于快速成员资格测试,例如代码if word in connectives: I also decided to use str.format to put the word into the string. 我还决定使用str.formatword放入字符串中。 This could be changed for word + ' ' if preferred. 如果愿意,可以将word + ' '更改为此选项。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM