Domanda

I have a file containing Unicode strings aligned line by line.

ജുഗുപ്‌സയോ നീരസമോ പരിഹാസമോ ദ്യോതിപ്പിക്കുന്ന മുഖഭാവം
വളവ്‌
വക്രത
തിരിവ്‌
കോട്ടം
നന്നേ ചെറുപ്രായത്തില്‍ അസാമന്യ ജീവിത വിജയം നേടുന്നയാള്‍
ഇന്റര്‍നെറ്റിലെ പ്രധാനപ്പെട്ട സേവനം
സ്‌ക്രീനില്‍ കാണുന്ന അതേ രൂപത്തിലും ഭാവത്തിലും പ്രിന്ററില്‍ നിന്ന്‌ ലഭിക്കുന്ന കോപ്പി
തെറ്റ്‌ എന്നു കാണിക്കുന്ന അടയാളം
യുണിക്‌സിനെ ആധാരമാക്കിയുള്ള പ്രവര്‍ത്തന കേന്ദ്രങ്ങളില്‍ ഉപയോഗപ്പെടുത്തുന്ന ഒരു നെറ്റവര്‍ക്ക്‌ വിന്‍ഡോ സ്ഥാപന അന്തരീക്ഷം
പ്രിന്ററിലൂടെ കടലാസ്‌ നീങ്ങിപ്പോകുന്ന ദിശക്ക്‌ ലംബമായുള്ള ദിശ
കമ്പ്യൂട്ടറിലെ ഒരു ഡിസ്‌കിലുള്ള വിവരങ്ങള്‍ മറ്റൊരു ഡിസ്‌കിലേക്ക്‌ കോപ്പിചെയ്‌തു വെക്കാന്‍ ഡോസ്‌ എന്ന ഓപ്പറേറ്റിംഗ്‌ സിസ്റ്റത്തിലുള്ള സംവിധാനം
ക്രിസ്‌തുമസ്‌
പ്രായപൂര്‍ത്തിയായവര്‍ക്കുള്ള ചലച്ചിത്രം
ചില പ്രത്യേക കിരണങ്ങളുടെ സഹായത്താല്‍ എടുക്കുന്ന ചിത്രങ്ങള്‍
എക്‌സറേ
അദൃശ്യാലക്തിക കിരണം
മരണം വരെയും സൗന്ദര്യം ഒരേപോലെ നിലനിര്‍ത്താന്‍ കഴിഞ്ഞവര്‍
കലഹപ്രിയ
ശണ്‌ഠക്കാരി

How to remove sentences from the file ?

I need to get

  ക്രിസ്‌തുമസ്‌
 കലഹപ്രിയ
    ശണ്‌ഠക്കാരി
വളവ്‌
    വക്രത
    തിരിവ്‌
    കോട്ടം

and remove all the sentences like this

പ്രിന്ററിലൂടെ കടലാസ്‌ നീങ്ങിപ്പോകുന്ന ദിശക്ക്‌ ലംബമായുള്ള ദിശ
    കമ്പ്യൂട്ടറിലെ ഒരു ഡിസ്‌കിലുള്ള വിവരങ്ങള്‍ മറ്റൊരു ഡിസ്‌കിലേക്ക്‌ കോപ്പിചെയ്‌തു വെക്കാന്‍ ഡോസ്‌ എന്ന ഓപ്പറേറ്റിംഗ്‌ സിസ്റ്റത്തിലുള്ള സംവിധാനം

They are separated by spaces.

I am using python 2.7

m = open('olam-enml.txt','w')

UTF8

When I tried this code

string = "നന്നേ ചെറുപ്രായത്തില്‍ അസാമന്യ ജീവിത വിജയം നേടുന്നയാള്‍"

if u' ' not in string .strip():
    print string

I got this error

Traceback (most recent call last):
  File "/home/akallararajappan/Music/Mycodeexp/d.py", line 3, in <module>
    if u' ' not in string .strip():
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 0: ordinal not in range(128)
È stato utile?

Soluzione

You can strip whitespace from the start and end of the lines, and if there are still spaces in the string you have a sentence:

if u' ' not in line.strip():
    # line is a *not* a sentence

Open your file with io.open() instead; to write just the lines that are not sentences you can use a simple generator expression:

import io

with io.open('olam-enml.txt', 'r', encoding='utf8') as infh:
    with io.open('olam-enml-words.txt', 'w', encoding='utf8') as outfh:
        outfh.writelines(line for line in infh if u' ' not in line.strip())
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top